【问题标题】:Select Xpath of button选择按钮的 Xpath
【发布时间】:2020-09-05 12:56:17
【问题描述】:

我拥有的以下检查代码是单击我可以确认订单的按钮

<button type="submit" class="button btn btn-default button-medium">
                <span>I confirm my order<i class="icon-chevron-right right"></i></span>
</button>
<span>I confirm my order<i class="icon-chevron-right right"></i></span>

我试过的那个:

driver.findElement(By.xpath("//button[contains(text(),'I confirm my order')]\"")).click();

使用 Eclipse 作为 Selenium 测试的 IDE 时出现以下错误:

org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //button[contains(text(),'I confirm my order')]" because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//button[contains(text(),'I confirm my order')]"' is not a valid XPath expression.

【问题讨论】:

    标签: java selenium xpath css-selectors webdriverwait


    【解决方案1】:

    试试这个:

    driver.find_element_by_xpath('//button[@type="submit"]/span[1]').click()
    

    【讨论】:

      【解决方案2】:

      文本我确认我的订单位于&lt;button&gt; 元素的子&lt;span&gt; 内。因此,对于元素上的click(),您可以使用以下任一Locator Strategies

      • cssSelector:

        driver.findElement(By.cssSelector("button.button.btn.btn-default.button-medium > span i.icon-chevron-right.right")).click();
        
      • xpath:

        driver.findElement(By.xpath("//button[@class='button btn btn-default button-medium'][./span[contains(., 'I confirm my order')]]")).click();
        

      但是,由于该元素是启用了JavaScript 的元素,因此要在该元素上使用click(),您需要为elementToBeClickable() 诱导WebDriverWait,并且您可以使用以下任一Locator Strategies

      • cssSelector:

        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.button.btn.btn-default.button-medium > span i.icon-chevron-right.right"))).click();
        
      • xpath:

        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='button btn btn-default button-medium'][./span[contains(., 'I confirm my order')]]"))).click();
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-27
        • 1970-01-01
        • 2021-09-29
        相关资源
        最近更新 更多