【问题标题】:How to select dropdown value in selenium webdriver using Testng?如何使用 Testng 在 selenium webdriver 中选择下拉值?
【发布时间】:2014-06-09 05:53:33
【问题描述】:

我们如何使用 Testng 在 selenium webdriver 中选择下拉值?

【问题讨论】:

  • 您的帖子中没有足够的详细信息,我们无法为您提供帮助。请为下拉列表添加 HTML,或使用下拉列表添加指向网站的链接。还包括您迄今为止尝试过的内容。

标签: selenium selenium-webdriver


【解决方案1】:

在 selenium webdriver 中选择下拉值不是 TestNG 的一部分,它是 selenium+java 代码的一部分。

使用下面的代码作为参考::

public class temp {
public static void main(String[] args) {

    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.shoppersstop.com/shoes/kids-shoes/all/brand/kittens.html");

    WebElement selectElement = driver.findElement(By
            .xpath("//select[@class='subCatThree' and @name='category']"));
    Select select = new Select(selectElement);
    List<WebElement> options = select.getOptions();

    for (WebElement option : options) {
        System.out.println(option.getText());
        if (option.getText().equals("Girls ( 3 Years & Above)")) {
            option.click();
            break;
        }
    }

}

【讨论】:

    【解决方案2】:

    如果要按值选择

    Select select = new Select(driver.findelement(By.xpath("write the xpath of dropdown")));
        select.selectByValue("write value here");
    

    如果要按文本选择

    Select select = new Select(driver.findelement(By.xpath("write the xpath of dropdown")));
        select.selectByVisibleText("write text here");
    

    【讨论】:

    • 请将selectByIndex 也添加到您的答案中。
    【解决方案3】:

    我们如何将此代码转换为使用 Selenium WebDriver

    If Browser("UOB").Page("pgeSetCustomLimits").Exist(intSyncTime*1) Then
        Set oDesc=Description.Create        
        oDesc("micclass").Value = "WebElement"
        oDesc("html id").Value = "limitsInput_CI_form_label_div"
        Set ObjEle =
             Browser("UOB").Page("pgeSetCustomLimits").ChildObjects(oDesc)
        For i=0 to ObjEle.count-1
            strWebEleText = ObjEle(i).getRoProperty("innertext")
            'print strWebEleText
            If Instr(strWebEleText,strPaymentType) Then
                intRow = i
                Exit For
            End If
        Next
    End If
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多