简体   繁体   English

如何使用Selenium WebDriver选择网页上的两个元素之一?

[英]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. 当浏览器打开新的URL时,我需要选择一个element_1,但是如果不存在,则必须选择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. 使用driver.findElement()进行隐式等待。 So even when secondElement appears - the test will wait for some time. 因此,即使出现secondElement,测试也将等待一段时间。 Instead using findElements() in a WebDriverWait might be a better solution. 而是在WebDriverWait中使用findElements()可能是更好的解决方案。

    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? 您可以尝试这样,是的,它正在使用try和catch块,但这是一个过程,因为除了isDisplayed(),isEnabled()和isSelected()之外,没有其他特殊方法isPresent(),出了什么问题无论如何用try和catch块处理异常?

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 . 您可以使用WATIR It does provide such a code if you are ok with Ruby . 如果您对Ruby它的确提供了这样的代码。 Otherwise, you just try and catch. 否则,您只是尝试抓住。

I think you can create a new method. 我认为您可以创建一种新方法。 Check this link: WebDriver: check if an element exists? 检查此链接: WebDriver:检查元素是否存在? This solution is faster. 此解决方案速度更快。

I think the better approach here is to use CSS3 selectors which can group selectors by using a comma : 我认为这里更好的方法是使用CSS3选择器,它可以使用逗号将选择器分组

Apologize for the python: 对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. 假设网页未显示,将显示<div class="not-found"> Or display the results with <tr class="entry"> 或使用<tr class="entry">显示结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Selenium WebDriver配置网页语言 - How to configure webpage language using selenium webdriver 如何使用Java来解析Selenium Webdriver中所有元素的网页,该网页会定期连续刷新4秒钟? - How to parse all the elements in Selenium Webdriver using Java for a webpage which is continously refreshing for 4 seconds periodically? 如何使用Selenium Webdriver正确查找网页的元素? - How does correctly find elements of a webpage with Selenium Webdriver? 如何使用 selenium webdriver 查找动态元素? - How to find a dynamic elements using selenium webdriver? 如何使用Selenium WebDriver捕获网页的屏幕截图? - How to capture the screen shot of a webpage using selenium webdriver? 如何使用Selenium Webdriver查找网页上多个按钮的数量 - How to find the count of multiple buttons on a webpage using Selenium Webdriver 如何使用Selenium Webdriver在新网页中查找元素? - How to find an element in new Webpage using selenium webdriver? 如何在网页上使用带有 Java 的 Selenium WebDriver 验证工具提示文本 - How to verify tooltip text using Selenium WebDriver with java on a webpage 如何使用Selenium WebDriver移至网页中的其他选项卡? - How to move to different tabs in a webpage using selenium webdriver? 使用Selenium WebDriver验证网页上的图像 - Validating image on a webpage using selenium webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM