选择按钮的xpath

pvcm50d1  于 2021-07-05  发布在  Java
关注(0)|答案(2)|浏览(715)

下面的检查代码我已经是点击按钮,我可以确认我的订单

<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.
5anewei6

5anewei61#

试试这个:

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

m0rkklqb2#

文本我确认我的命令是在孩子 <span><button> 元素。所以,为了 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();

相关问题