【问题标题】:How to identify the element using Selenium and Java如何使用 Selenium 和 Java 识别元素
【发布时间】:2020-06-18 06:32:19
【问题描述】:

我正在尝试识别元素。我想确定“解决您的担忧”。我可以到达 div 标签,但我需要到达 div 标签中的文本。

HTML:

<span class="Linkify">
    <div>
        <div>
            address your concerns. 
        </div> 
    </div>
</span>

我正在尝试识别文本“解决您的问题”,所以任何人都可以在 chrome 浏览器中帮助我解决这个问题,我正在尝试吗?

【问题讨论】:

    标签: java selenium xpath css-selectors webdriverwait


    【解决方案1】:

    要使用Selenium 识别元素,您需要为visibilityOfElementLocated() 诱导WebDriverWait,您可以使用以下Locator Strategies 之一:

    • cssSelector

      WebElement cssElement = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("span.Linkify > div > div")));
      
    • xpath

      WebElement xpathElement = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@class='Linkify']/div/div[contains(., 'address your concerns')]")));
      

    【讨论】:

      【解决方案2】:

      尝试使用 XPath 来定位您的 div 元素,例如“//span[@class='Linkify']/div/div”,然后在此 div 上使用 getText() 方法来获取您的文本。

      【讨论】:

      • //span[@class='Linkify']/div/div[contains(text(), '解决您的干眼问题。')] 我需要不使用 getText() 方法最多可以到达第二个 div 标签
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-14
      相关资源
      最近更新 更多