【问题标题】:How to select an element when either of the elements can be found using Selenium and Java当可以使用 Selenium 和 Java 找到任何一个元素时如何选择一个元素
【发布时间】:2019-05-02 08:58:53
【问题描述】:

每个负载的数量按钮设计都不同。我已经确定了他们 但我想在找到任何一个元素时继续。

//select quantity 2
//sometimes button A appear
driver.findElement(By.xpath("//select[@id='quantity']")).click();
Select quantity = new Select(driver.findElement(By.xpath("//select[@id='quantity']")));
quantity.selectByIndex(1); 

//sometimes button B appear
driver.findElement(By.xpath("//select[@id='amt']")).click();
Select amt = new Select(driver.findElement(By.xpath("//select[@id='amt']")));
quantity.selectByIndex(1); 

【问题讨论】:

  • 两个按钮是否同时出现,还是一个按钮显示而另一个按钮不在页面上?

标签: selenium selenium-webdriver select webdriver webdriverwait


【解决方案1】:

您可以使用findElements() 代替findElement(),这将返回一个网络元素列表。

现在,如果大小为 1,您的脚本将知道该特定元素存在。如果 size id 0,那么按钮在 UI 中将不可见。

数量按钮是这样的:

List<WebElement> quantityButton = driver.findElements(By.xpath("//select[@id='quantity']"));
if(quantityButton.size()==1){
   quantityButton.get(0).click();
}  

对于 amt 按钮:

List<WebElement> amtButton = driver.findElements(By.xpath("//select[@id='amt']"));
    if(amtButton.size()==1){
       amtButton.get(0).click();
    }  

您可以根据需要编写相应的 else 块。

不同的方法是使用 try-catch 块。

如果有帮助,请告诉我。

【讨论】:

  • 我不确定逻辑。如何让循环点击其中一个?
  • @user2201789 :两个定位器都不同,我不认为你可以在这里和任何情况下都有循环......你可以在这里做的唯一一件事就是尝试和捕捉块。提出这个问题,让我们看看我们是否对此有更多的了解。
  • @cruisepandey : 你可以使用嵌套循环。
  • @KunduK :可能是一个镜头。如果您可以将其写为答案,那将对OP非常有帮助。
【解决方案2】:

由于 Cruisepandey 已经提供了如何处理这种情况的逻辑。对于所有的困惑,你可以尝试嵌套 if..else 循环,它会检查第一个元素 size() 是否出现 0 如果会进入另一个循环并检查第二个元素 size()。

if(driver.findElements(By.xpath("//select[@id='quantity']")).size()==0)
{
 if(driver.findElements(By.xpath("//select[@id='amt']")).size()>0)
 {
   driver.findElements(By.xpath("//select[@id='amt']")).get(0).click(); 
   Select amt = new Select(driver.findElement(By.xpath("//select[@id='amt']")));
   amt.selectByIndex(1); 
 }
 else
 {
   System.out.println("None of the elements present")
 }

}
else
{
  driver.findElements(By.xpath("//select[@id='quantity']")).get(0).click();
  Select quantity = new Select(driver.findElement(By.xpath("//select[@id='quantity']")));
  quantity.selectByIndex(1); 

}       

【讨论】:

    【解决方案3】:

    正如您提到的找到一个元素大概该元素是一个动态元素,您需要为elementToBeClickable() 诱导 WebDriverWait,您可以使用以下任一定位器策略:

    • 使用xpath

      WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.or(
          ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='quantity']")),
          ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='amt']"))
      ));
      Select amt = new Select(element);
      quantity.selectByIndex(1); 
      
    • 使用cssSelector

      WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.or(
          ExpectedConditions.visibilityOfElementLocated(By.cssSelector("select#quantity")),
          ExpectedConditions.visibilityOfElementLocated(By.cssSelector("select#amt"))
      ));
      Select quantity = new Select(element);
      quantity.selectByIndex(1); 
      

    【讨论】:

      【解决方案4】:

      如果只有一个按钮(实际上“选择”在这里是合适的),那么您可以修改您的 XPath 以选择任一元素:

      WebElement quantityElement = driver.findElement(By.xpath("//select[@id='quantity' or @id='amt']"));
      Select quantity = new Select(quantityElement);
      
      quantityElement.click();
      quantity.selectByIndex(1);
      

      那么无论哪个出现在屏幕上都没有关系://select[@id='quantity' or @id='amt'] — 它会匹配任一 HTML 标签 ID。

      如果页面中同时存在两个 HTML 标签,但只有一个标签同时可见,则此方法无效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-09
        • 2018-09-15
        • 1970-01-01
        相关资源
        最近更新 更多