当可以使用 Selenium 和 Java 找到任一元素时,如何 select 一个元素

How to select an element when either of the elements can be found using Selenium and Java

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

//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); 

您可以使用 findElements() 而不是 findElement(),那将是 return 网络元素列表。

现在,如果大小为 1,您的脚本将知道存在特定元素。如果尺寸 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 块。

如果有帮助请告诉我。

由于 cruispandey 已经提供了如何处理这种 scenario.For 你所有的困惑,你可以尝试嵌套 if..else 循环,它将检查第一个元素 size() 是否出现 0 if 将进入另一个循环并检查第二个元素 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); 

}       

正如您提到的那样找到一个元素 大概该元素是一个动态元素,您需要为 [=12] 引入 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); 
    

如果只有一个按钮(实际上 "select" 在这里是合适的)那么您可以修改您的 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 标签,这将不起作用,但同时只有一个可见。