简体   繁体   English

Selenium WebDriver:检查是否存在元素一段时间循环?

[英]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.

相关问题 如果我要检查Selenium Webdriver中是否存在该元素,该怎么办? - What can be used if I want to check if the element is present or not in selenium webdriver? 使用带有 Java 的 Selenium WebDriver 检查元素不存在的最佳方法 - Best way to check that element is not present using Selenium WebDriver with java Selenium 2 - 如何在隐式等待时检查元素是否不存在? - Selenium 2 - How to check if element not present while implicitly waiting? 硒WebDriver上的“ isDisplyed”无法检查WebElement是否存在 - Cannot check the WebElement is Present or Not by “isDisplyed” on selenium WebDriver 使用 Selenium WebDriver 测试元素是否存在 - Test if an element is present using Selenium WebDriver 使用Java检查Selenium WebDriver在页面上是否存在或显示元素的正确方法是什么? - Which is the correct way to check if an element is present or displayed on a page with Selenium WebDriver using Java? 如何验证硒2中存在或可见的元素(Selenium WebDriver) - How to verify element present or visible in selenium 2 (Selenium WebDriver) 硒:检查任何时间存在的元素 - Selenium : Check an Element present at any point of time 硒-如何检查元素是否存在 - Selenium- How to check if element is present or not 当元素属性不存在时,使用java在Selenium WebDriver中按ENTER键 - Press ENTER key in Selenium WebDriver with java when element property not present
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM