【问题标题】:Select the values from two dyanamic dropdowns从两个动态下拉列表中选择值
【发布时间】:2019-06-06 11:41:25
【问题描述】:

我正在尝试使用 java selenium Web 驱动程序从站点 https://www.xe.com/currencyconverter/ 的两个下拉列表(货币选择)中选择值。

货币转换后我还需要验证转换的代码

我试过了:

1. Custom XPath.
2. Normal select using select keyword.
3. Tried using mouse actions but couldn't locate. Looks like it needs javascript executor but don't know the code.

org.openqa.selenium.ElementClickInterceptedException:元素点击被拦截:元素<div class="css-1wy0on6 converterform-dropdown__indicators">...</div>不可点击

【问题讨论】:

    标签: javascript java selenium-webdriver dropdown


    【解决方案1】:

    很可能你有这个Cookie Consent Disclaimer 拦截你的输入事件,因此你不能有效地点击下拉值。

    Selenium Java API 你最好的朋友是WebDriverWait 类:

    WebDriverWait wait = new WebDriverWait(driver, 5); 
    

    我建议通过上述 WebDriverWait 与 ExpectedConditions 一起使用 Explicit Wait,以便:

    1. 首先确保cookie免责声明可见并且可以点击然后点击:

      wait.until(ExpectedConditions
              .elementToBeClickable(
                      By.xpath("//button[contains(@class,'privacy') and contains(text(), 'OK')]")))
              .click();
      
    2. 然后对第一个下拉菜单执行相同的操作:

      wait.until(ExpectedConditions
              .elementToBeClickable(
                      By.id("from")))
              .click();
      
    3. 最后通过再次点击选择您选择的货币:

      wait.until(ExpectedConditions
              .elementToBeClickable(
                      By.xpath("//span[contains(text(),'GBP')]")))
              .click();
      

      GBP替换为您需要的源货币

    【讨论】:

    • 谢谢。但是两个下拉菜单都具有相同的 div 和相同的自定义 xpath。我们如何区分?
    • 我明白了。谢谢
    • @Dmitri,在第 3 步,您如何知道下拉值标签名称是否为 <span>????也可能是<li> 对吗?根据输入type = hidden,你怎么会知道那个标签名称?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 2020-02-24
    • 2018-12-16
    • 1970-01-01
    • 2015-09-07
    相关资源
    最近更新 更多