选择按钮的Xpath

huangapple go评论103阅读模式
英文:

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&#39;ve tried:

```
driver.findElement(By.xpath(&quot;//button[contains(text(),&#39;I confirm my order&#39;)]\&quot;&quot;)).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(),&#39;I confirm my order&#39;)]&quot; because of the following error:
    SyntaxError: Failed to execute &#39;evaluate&#39; on &#39;Document&#39;: The string &#39;//button[contains(text(),&#39;I confirm my order&#39;)]&quot;&#39; 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(&#39;//button[@type=&quot;submit&quot;]/span[1]&#39;).click()

</details>



# 答案2
**得分**: 0

以下是翻译好的部分:

**我确认我的订单** 文本位于 `&lt;button&gt;` 元素的子元素 `&lt;span&gt;` 中。因此,要对该元素执行 `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 `&lt;span&gt;` of the `&lt;button&gt;` 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(&quot;button.button.btn.btn-default.button-medium &gt; span i.icon-chevron-right.right&quot;)).click();
           	
- `xpath`:

	  driver.findElement(By.xpath(&quot;//button[@class=&#39;button btn btn-default button-medium&#39;][./span[contains(., &#39;I confirm my order&#39;)]]&quot;)).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(&quot;button.button.btn.btn-default.button-medium &gt; span i.icon-chevron-right.right&quot;))).click();
           	
- `xpath`:

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

</details>



huangapple
  • 本文由 发表于 2020年9月5日 20:56:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63754162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定