Select 按钮的Xpath

Select Xpath of button

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

<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 作为 IDE 进行如下 Selenium 测试时出现错误:

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.

试试这个:

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

文本 我确认我的订单<button> 元素的子元素 <span> 中。因此,要在元素上 click() 您可以使用以下任一 :

  • 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() 并且您可以使用以下任一项 :

  • 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();