首页 > 解决方案 > 如何解决代码中“等待 By.xpath 定位的元素的可见性”?

问题描述

我的程序未能找到pass-code登录页面下一页中的字段,因此我尝试通过等待来定位它,如下所示(一段代码显示)。

我已经尝试将等待时间增加到 100,但它也不起作用。

即使没有到达sing-in按钮,它也会显示为程序在密码字段中停止。

收到的错误如下:

org.openqa.selenium.TimeoutException:预期条件失败:等待 By.xpath 定位的元素的可见性://input[@id='user-passcode'](以 500 毫秒间隔尝试 10 秒)在org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:81)

它总是显示它在 81 中失败,无论代码...

WebElement unfield = driver.findElement(By.xpath("//input[@id='user-name']"));
Actions actions = new Actions(driver);
actions.moveToElement(unfield).click();     
unfield.clear();
unfield.sendKeys("test");      
driver.findElement(By.xpath("//input[@id='user-password']")).clear();
driver.findElement(By.xpath("//input[@id='user-password']")).sendKeys("test");
WebElement test = driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='SIGN IN'])[1]/following::button[1]"));
Actions actions_signinclick = new Actions(driver);
signinclick_buttonclick .moveToElement(test).click();
//this will display in next page
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='user-passcode']")));
driver.findElement(By.xpath("//input[@id='user-passcode']")).click();
driver.findElement(By.xpath("//input[@id='user-passcode']")).clear();
driver.findElement(By.xpath("//input[@id='user-passcode']")).sendKeys("1234");      
WebDriverWait submit_button = new WebDriverWait(driver, 100);
submit_button.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//BUTTON[@_ngcontent-wks-c3=''][text()='SUBMIT']/self::BUTTON")));
driver.findElement(By.xpath("//BUTTON[@_ngcontent-wks-c3=''][text()='SUBMIT']/self::BUTTON")).click();

请帮助我找到解决方案。

标签: testingselenium-webdriverbrowser-automation

解决方案


请试试这个。

更改此行:

signinclick_buttonclick .moveToElement(test).click();

成为 :

signinclick_buttonclick.moveToElement(test).click().build().perform();

然后将定位器更改//input[@id='user-passcode']//*[@id='user-passcode']这样:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='user-passcode']")));
driver.findElement(By.xpath("//*[@id='user-passcode']")).click();

推荐阅读