testing - 如何解决代码中“等待 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();
请帮助我找到解决方案。
解决方案
请试试这个。
更改此行:
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();
推荐阅读
- python - Kivy 返回 AttributeError: object has no attribute 'name' when it clear has it
- java - 警告:RPC 失败:尝试在 java 和 Nodejs 中使用 GRPC 时,状态{code=NOT_FOUND,description=Not found,cause=null}
- go - 如何使用 Apache Thrift 定义类型 mgo objectId?
- powershell - Powershell - Start-Job - 传递基于变量的命令
- node.js - 如何让 Nodejs 在 CORS 中使用 k8 DNS 命名
- ruby-on-rails - 在 Rails 的顶级路径和嵌套路径中使用具有关联的相同控制器
- python - Python 包安装在 AmazonLinux 上
- c++ - 从一类调用函数到第二类导致串行 C++ 上的编码错误
- python - 函数总是返回 false
- c - 尝试释放 malloc 时出错