Selenium:尝试在 https://www.phptravels.net/ 上单击带有文本作为我的帐户的元素时出现 ElementNotVisibleException
Selenium: ElementNotVisibleException while trying to click the element with text as My Account onhttps://www.phptravels.net/
当我测试 phptravel 网站并尝试使用以下代码点击 myaccount link 时。
Selenium returns ElementNotVisibleException 在执行期间。我错过了什么?
源代码
public void login(WebDriver driver) {
driver.navigate().to("https://www.phptravels.net/");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/nav/div/div[1]/a")));
// Error on here
myAccount.click();
WebDriverWait myAccountWait = new WebDriverWait(driver, 10);
myAccountWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"li_myaccount\"]/ul")));
loginLink.click();
WebDriverWait loginWait = new WebDriverWait(driver, 10);
//loginWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\\"loginfrm\\"]/div[1]/div[5]/div/div[1]/input")));
username.sendKeys("user@phptravels.com");
password.sendKeys("demouser");
loginBtn.click();
}
修改代码如下:
在您的 WebDriverWait 上,使用 By 类型保持 xpath 如下:
By myAccountBy = By.xpath("//ul[@class='nav navbar-nav navbar-right']/ul/li[1]/a");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(myAccountBy));
或
像下面这样对 xpath 进行硬编码。
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.Xpath("//ul[@class='nav navbar-nav navbar-right']/ul/li[1]/a")));
然后为 myAccount WebElement 保留相同的 xpath,如下所示
@FindBy(xpath="//ul[@class='nav navbar-nav navbar-right']/ul/li[1]/a")
public WebElement myAccount;
简而言之,要单击 MyAccount,您必须保留此 xpath
//ul[@class='nav navbar-nav navbar-right']/ul/li[1]/a
myAccount webElement 未在您的代码中初始化。
如果您想点击“我的帐户”link,您可以使用:
WebElement myAccount = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='collapse']/descendant::ul[3]/li[@id='li_myaccount']/a")));
myAccount.click();
请注意,您不能使用 link Text,因为那是 text nodes .
首先您需要创建 WebElement
,然后使用 elementToBeClickable
预期条件,这样您就可以解决问题
WebElement myAccount = driver.findElement("Your locator");
现在使用 wait
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(myAccount));
myAccount.click();
同时最大化浏览器。
加载 URL 后添加显式等待始终是最佳做法。我可以根据以下修改后的 XPath 单击帐户 link。
Xpath: //nav//li[@id='li_myaccount']//a
工作代码:
driver.get("https://www.phptravels.net/");
WebDriverWait wait=new WebDriverWait(driver,10);
//wait is added in order to complete the page loading
wait.until(ExpectedConditions.titleContains("PHPTRAVELS"));
driver.findElement(By.xpath("//nav//li[@id='li_myaccount']//a")).click();
To click()
在文本为 My Account 而不是 visibilityOfElementLocated()
的元素上,您需要引入 WebDriverWait通过elementToBeClickable()
方法,可以使用如下解决方法:
代码块:
System.setProperty("webdriver.gecko.driver", "C:\Utility\BrowserDrivers\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://www.phptravels.net");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//nav[@class='navbar navbar-default']//li[@id='li_myaccount']/a[normalize-space()='My Account']"))).click();
浏览器快照:
当我测试 phptravel 网站并尝试使用以下代码点击 myaccount link 时。 Selenium returns ElementNotVisibleException 在执行期间。我错过了什么?
源代码
public void login(WebDriver driver) {
driver.navigate().to("https://www.phptravels.net/");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/nav/div/div[1]/a")));
// Error on here
myAccount.click();
WebDriverWait myAccountWait = new WebDriverWait(driver, 10);
myAccountWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"li_myaccount\"]/ul")));
loginLink.click();
WebDriverWait loginWait = new WebDriverWait(driver, 10);
//loginWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\\"loginfrm\\"]/div[1]/div[5]/div/div[1]/input")));
username.sendKeys("user@phptravels.com");
password.sendKeys("demouser");
loginBtn.click();
}
修改代码如下:
在您的 WebDriverWait 上,使用 By 类型保持 xpath 如下:
By myAccountBy = By.xpath("//ul[@class='nav navbar-nav navbar-right']/ul/li[1]/a");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(myAccountBy));
或
像下面这样对 xpath 进行硬编码。
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.Xpath("//ul[@class='nav navbar-nav navbar-right']/ul/li[1]/a")));
然后为 myAccount WebElement 保留相同的 xpath,如下所示
@FindBy(xpath="//ul[@class='nav navbar-nav navbar-right']/ul/li[1]/a")
public WebElement myAccount;
简而言之,要单击 MyAccount,您必须保留此 xpath
//ul[@class='nav navbar-nav navbar-right']/ul/li[1]/a
myAccount webElement 未在您的代码中初始化。
如果您想点击“我的帐户”link,您可以使用:
WebElement myAccount = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='collapse']/descendant::ul[3]/li[@id='li_myaccount']/a")));
myAccount.click();
请注意,您不能使用 link Text,因为那是 text nodes .
首先您需要创建 WebElement
,然后使用 elementToBeClickable
预期条件,这样您就可以解决问题
WebElement myAccount = driver.findElement("Your locator");
现在使用 wait
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(myAccount));
myAccount.click();
同时最大化浏览器。
加载 URL 后添加显式等待始终是最佳做法。我可以根据以下修改后的 XPath 单击帐户 link。
Xpath: //nav//li[@id='li_myaccount']//a
工作代码:
driver.get("https://www.phptravels.net/");
WebDriverWait wait=new WebDriverWait(driver,10);
//wait is added in order to complete the page loading
wait.until(ExpectedConditions.titleContains("PHPTRAVELS"));
driver.findElement(By.xpath("//nav//li[@id='li_myaccount']//a")).click();
To click()
在文本为 My Account 而不是 visibilityOfElementLocated()
的元素上,您需要引入 WebDriverWait通过elementToBeClickable()
方法,可以使用如下解决方法:
代码块:
System.setProperty("webdriver.gecko.driver", "C:\Utility\BrowserDrivers\geckodriver.exe"); WebDriver driver=new FirefoxDriver(); driver.get("http://www.phptravels.net"); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//nav[@class='navbar navbar-default']//li[@id='li_myaccount']/a[normalize-space()='My Account']"))).click();
浏览器快照: