英文:
Select Xpath of button
问题
以下是您要的翻译部分:
以下是我要点击的按钮的检查代码,以确认我的订单
我确认我的订单
```
我尝试过的代码:
driver.findElement(By.xpath("//button[contains(text(),'我确认我的订单')]")).click();
在使用Eclipse作为Selenium测试的IDE时,出现了以下错误:
org.openqa.selenium.InvalidSelectorException: 无效的选择器:无法定位具有XPath表达式//button[contains(text(),'我确认我的订单')]的元素,原因如下:
SyntaxError: 未能在Document上执行'evaluate':字符串'//button[contains(text(),'我确认我的订单')]'不是有效的XPath表达式。
<details>
<summary>英文:</summary>
The following inspect code i have is to click the button I can confirm my order
<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>
The one I've tried:
```
driver.findElement(By.xpath("//button[contains(text(),'I confirm my order')]\"")).click();
```
The error is coming out using Eclipse as IDE for Selenium Testing stated below:
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.
</details>
# 答案1
**得分**: 0
请尝试这样做:
```python
driver.find_element_by_xpath('//button[@type="submit"]/span[1]').click()
```
<details>
<summary>英文:</summary>
Try this:
driver.find_element_by_xpath('//button[@type="submit"]/span[1]').click()
</details>
# 答案2
**得分**: 0
以下是翻译好的部分:
**我确认我的订单** 文本位于 `<button>` 元素的子元素 `<span>` 中。因此,要对该元素执行 `click()` 操作,您可以使用以下任一[定位策略](https://stackoverflow.com/questions/48369043/official-locator-strategies-for-the-webdriver/48376890#48376890):
- `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(., '我确认我的订单')]]")).click();
然而,由于该元素是一个启用了[JavaScript](https://www.javascript.com/)的元素,因此要对其执行 `click()` 操作,您需要使用[WebDriverWait](https://stackoverflow.com/questions/48989049/selenium-how-selenium-identifies-elements-visible-or-not-is-is-possible-that-i/48990165#48990165)来等待 `elementToBeClickable()`,您可以使用以下任一[定位策略](https://stackoverflow.com/questions/48369043/official-locator-strategies-for-the-webdriver/48376890#48376890):
- `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(., '我确认我的订单')]]"))).click();
<details>
<summary>英文:</summary>
The text **I confirm my order** is within the child `<span>` of the `<button>` element. So, to `click()` on the element you can use either of the following [Locator Strategies](https://stackoverflow.com/questions/48369043/official-locator-strategies-for-the-webdriver/48376890#48376890):
- `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();
However, as the element is a [JavaScript](https://www.javascript.com/) enabled element so to `click()` on the element you need to induce [WebDriverWait](https://stackoverflow.com/questions/48989049/selenium-how-selenium-identifies-elements-visible-or-not-is-is-possible-that-i/48990165#48990165) for the `elementToBeClickable()` and you can use either of the following [Locator Strategies](https://stackoverflow.com/questions/48369043/official-locator-strategies-for-the-webdriver/48376890#48376890):
- `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();
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论