使用@FindBy注解调用JS点击元素时触发WebDriverException求助
Let's tackle this issue head-on. The error you're seeing:
org.openqa.selenium.WebDriverException: unknown error: Cannot read property 'click' of null
means one clear thing: the SelectTitleDropDown element you're getting via @FindBy is null. When you pass a null value to your ClickByJS method, the JavaScript can't execute click() on nothing—hence the error.
Here are the most likely fixes, ordered by how common they are:
1. Fix Your XPath Typo (Most Probable Cause)
Look closely at the two XPath expressions you're using:
- Working direct
findElementXPath://div[@aria-owns='registrationPageTitleSelect-dropdown-list'] - Broken
@FindByXPath://div[@aria-owns='registrationPageTitleSelect-dropdown- list']
Notice the extra space in the @FindBy version: dropdown- list instead of dropdown-list. That tiny space makes the XPath match nothing, so your SelectTitleDropDown element stays null. Correct the XPath:
@FindBy(xpath="//div[@aria-owns='registrationPageTitleSelect-dropdown-list']") WebElement SelectTitleDropDown;
2. Make Sure PageFactory Initializes Your Elements
The @FindBy annotation doesn't work on its own—you need to tell Selenium to initialize those elements using PageFactory.initElements(). Add this to your page class's constructor:
// Inside your page class private WebDriver driver; public YourRegistrationPage(WebDriver driver) { this.driver = driver; // This line initializes all @FindBy annotated elements PageFactory.initElements(driver, this); }
When you use the page class, make sure you instantiate it with your driver:
YourRegistrationPage registrationPage = new YourRegistrationPage(driver); registrationPage.ValidateSelectTitle();
3. Wait for the Element to Load Before Clicking
Even if your XPath is correct and elements are initialized, the element might not be loaded on the page yet when you try to click it. Add an explicit wait to ensure the element is ready:
public void ValidateSelectTitle() { // Wait up to 10 seconds for the element to be clickable WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); wait.until(ExpectedConditions.elementToBeClickable(SelectTitleDropDown)); // Now run the JS click ClickByJS(SelectTitleDropDown, driver); }
You can also make your ClickByJS method more robust by adding the wait inside it:
public static void ClickByJS(WebElement element, WebDriver driver){ WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); wait.until(ExpectedConditions.visibilityOf(element)); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].click();", element); }
4. Debug with a Null Check
If you're still stuck, add a quick debug check to confirm if the element is null before trying to click it:
public void ValidateSelectTitle() { if (SelectTitleDropDown == null) { System.out.println("Element is null! Re-finding it manually."); // Fallback to direct findElement if needed SelectTitleDropDown = driver.findElement(By.xpath("//div[@aria-owns='registrationPageTitleSelect-dropdown-list']")); } ClickByJS(SelectTitleDropDown, driver); }
内容的提问来源于stack exchange,提问作者Shakamuri Shaka

