[英]Selenium WebDriver: Check if element present or not for a while loop?
Suppose there is page in which I want to check if an element "abc" is present on the page or not. 假设有一个页面我要检查页面上是否存在元素“ abc”。 If the element is not present then I want to click on some other element "xyz", and then new page opens up and again I want to check if the element "abc" is present on the page.
如果不存在该元素,那么我想单击其他一些元素“ xyz”,然后打开新页面,然后再次检查该页面上是否存在元素“ abc”。 If again not present then click on the same element "xyz" and this goes on.
如果再次不存在,则单击同一元素“ xyz”,然后继续。
I used while loop and below is the code. 我使用了while循环,下面是代码。 Please help me out.
请帮帮我。
In my below code: "elem_prev" is the element I want to check every time if it is present on the page or not. 在我下面的代码中:“ elem_prev”是我每次要检查页面上是否存在的元素。 My code is not working, now it is not able to find the element in the loop, but it should be as it is present on every page.
我的代码无法正常工作,现在它无法在循环中找到该元素,但它应该存在于每个页面上。 Please help me with the concepts as well.
也请帮助我了解这些概念。 Is the code correct?
代码正确吗?
It says for the element present in the loop that Unable to locate Element. 对于循环中存在的元素,它表示无法定位元素。
Boolean elem_prev = driver.findElements(By.id("TM_TL_DD_WRK_GET_DATA")).size()!=0;
while(elem_prev==false)
{
Thread.sleep(2000);
driver.findElement(By.className("PSHYPERLINK")).click();
Thread.sleep(2000);
Alert a = driver.switchTo().alert();
a.dismiss();
Thread.sleep(10000);
elem_prev = driver.findElements(By.id("TM_TL_DD_WRK_GET_DATA")).size()!=0;
}
I suggest you find the elements out of the loop like this: 我建议您像这样从循环中找到元素:
WebElement element1 = driver.findElements(By.id("TM_TL_DD_WRK_GET_DATA"));
WebElement element2 = driver.findElement(By.className("PSHYPERLINK"));
while(element1==false){
Thread.sleep(2000);
element2.click();
Thread.sleep(2000);
Alert a = driver.switchTo().alert();
a.dismiss();
Thread.sleep(10000);
element1.size()!=0;
}
and then try a SystemOutPrintln(element1);
然后尝试
SystemOutPrintln(element1);
and/or SystemOutPrintln(element2);
和/或
SystemOutPrintln(element2);
before the loop starts to be sure it actually finds the element. 在开始循环之前,请确保它确实找到了元素。
If it doesn't print an element, then the problem is not the loop, but the way you try to find the element. 如果它不显示元素,则问题不在于循环,而在于您尝试查找元素的方式。
This would be important information as you can then try to find the element in other way. 这将是重要的信息,因为您随后可以尝试以其他方式查找元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.