Java Selenium Web驱动程序If Else条件在两个按钮之间进行选择
我正在为一个网页编写一个自动化测试,在这个测试中,用户必须根据它的存在点击一个按钮 我将SeleniumWebDriver与Java一起使用 脚本 1.一次只能出现一个按钮(Java Selenium Web驱动程序If Else条件在两个按钮之间进行选择,java,if-statement,selenium-webdriver,Java,If Statement,Selenium Webdriver,我正在为一个网页编写一个自动化测试,在这个测试中,用户必须根据它的存在点击一个按钮 我将SeleniumWebDriver与Java一起使用 脚本 1.一次只能出现一个按钮(button1或button2) 2.如果出现按钮1,则用户单击该按钮 3.如果存在按钮2,则用户单击该按钮 代码: 上面的代码片段不起作用。有人能帮我纠正一下吗 注意:如果我删除If-else条件并为每个按钮功能单独运行,它将工作 谢谢,在if-else逻辑之前,请尝试使用显式等待 WebDriverWait wait =
button1
或button2
)
2.如果出现按钮1
,则用户单击该按钮
3.如果存在按钮2
,则用户单击该按钮
代码:
上面的代码片段不起作用。有人能帮我纠正一下吗
注意:如果我删除If-else条件并为每个按钮功能单独运行,它将工作
谢谢,在if-else逻辑之前,请尝试使用显式等待
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div")));
如果有帮助,请告诉我。谢谢所有帮助过我的人。如果将来有人需要的话,我会在这里发布一段代码 情景: -该网页有两个按钮-按钮1和按钮2。 -按钮1最初将出现 -如果用户最初单击按钮1,则会出现第二次按钮2。(在本例中,按钮1对UI隐藏) 代码块:
if(!driver.findElements(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[3]/div[2]/div")).isEmpty()){
//Clicking on button2, which appears when button1 is not present
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[3]/div[2]/div")));
Thread.sleep(5000);
driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[3]/div[2]/div")).click();
}
else if(driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div")).isEnabled() ){
//clicking on button1
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div")));
driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div")).click();
}
else
{
System.out.println("No such button found");
}
Thread.sleep(5000);
什么不起作用?上面写着“找不到这样的按钮”吗?是的。它抛出错误“org.openqa.selenium.NoSuchElementException:no-thick元素:”如果我在没有If-else条件的情况下为两个按钮分别运行代码,那么这些xpath表达式非常长。你能证实他们能工作吗?你能通过给按钮添加一个id来简化它们吗?即使没有显示,这两个按钮是否都存在于文档中?我之所以这样问,是因为例外情况似乎表明其中一项甚至不在文件中。但也许你已经想到了……在这种情况下:检查它们是否存在,(),然后检查它们是否显示:对不起,这也不起作用。它抛出相同的错误元素not Found,但这段代码有效-if(!driver.findElements(By.xpath(“xpath”)).isEmpty()
if(!driver.findElements(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[3]/div[2]/div")).isEmpty()){
//Clicking on button2, which appears when button1 is not present
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[3]/div[2]/div")));
Thread.sleep(5000);
driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[3]/div[2]/div")).click();
}
else if(driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div")).isEnabled() ){
//clicking on button1
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div")));
driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div")).click();
}
else
{
System.out.println("No such button found");
}
Thread.sleep(5000);