简体   繁体   中英

How to chose either of 2 elements on a Webpage using Selenium WebDriver?

I am running a test on a web page that may or may not have a certain element. The procedure is the following: I fill out the form, then click "Search" button. When the browser opens a new url, I need to choose an element_1, but if it does not exist I must choose element_2.

WebElement element = driver.findElement(By.xpath("//....")) || driver.findElement(By.xpath("//..."));
element.click();

// This does not do the trick, is there any other way? Is it possible to do without try and catch?

Using driver.findElement() does the implicit wait. So even when secondElement appears - the test will wait for some time. Instead using findElements() in a WebDriverWait might be a better solution.

    WebDriverWait wait = new WebDriverWait(driver, 10);
    // Wait for one element to be available
    wait.until(new Predicate<WebDriver>() {
        @Override public boolean apply(WebDriver driver) {
            return driver.findElements(By.cssSelector("xpath1")).size() > 0
                    || driver.findElements(By.cssSelector("xpath2")).size() > 0;
        }
    });
    // Get the element
    WebElement e = null;
    if (driver.findElements(By.cssSelector("xpath1")).size() > 0)
        e = driver.findElements(By.cssSelector("xpath1")).get(0);
    else if (driver.findElements(By.cssSelector("xpath2")).size() > 0)
        e = driver.findElements(By.cssSelector("xpath1")).get(0);

    if (e != null)
        e.click();

You can try something like this, yes, it is using try and catch blocks but this is the procedure one would follow as there is no special method isPresent() other than isDisplayed(), isEnabled() and isSelected() and what is wrong in handling the exception with try and catch block anyway?

boolean isElement1Present = true;

try{       
      driver.findElement(By.xpath("xpathExpression1"));    
}catch (NoSuchElementException e){
    isElement1Present = false;
}

if(isElement1Present == false)
     driver.findElement(By.xpath("xpathExpression2"));

Or Using the below avoids try and catch block.

List<WebElement> elements = driver.findElements(By.xpath("xpathExpression1"));   

if (elements.size() == 0) {
    System.out.println("Element is not present"); 
} else {
    System.out.println("Element is present");
}

This might help you

 WebElement element = null;

try{
    element = driver.findElement(By.xpath("//path1/...."));
}catch(NoSuchElementException ex){
    try{
        element = driver.findElement(By.xpath("//path2/...."));
    }catch(NoSuchElementException ex2){}
} 

if(element!=null) element.click();

Actually there is no explicit way. You can use WATIR . It does provide such a code if you are ok with Ruby . Otherwise, you just try and catch.

I think you can create a new method. Check this link: WebDriver: check if an element exists? This solution is faster.

I think the better approach here is to use CSS3 selectors which can group selectors by using a comma :

Apologize for the python:

element = WebDriverWait(driver, 2).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, "tr.entry , div.not-found"))
)

Assuming the web page would display a <div class="not-found"> in case nothing is found. Or display the results with <tr class="entry">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM