选择按钮的Xpath

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

Select Xpath of button

问题

以下是您要的翻译部分:

  1. 以下是我要点击的按钮的检查代码,以确认我的订单


我确认我的订单
```

我尝试过的代码:

  1. 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表达式。

  1. <details>
  2. <summary>英文:</summary>
  3. 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>

  1. The one I&#39;ve tried:
  2. ```
  3. driver.findElement(By.xpath(&quot;//button[contains(text(),&#39;I confirm my order&#39;)]\&quot;&quot;)).click();
  4. ```
  5. The error is coming out using Eclipse as IDE for Selenium Testing stated below:
  6. 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:
  7. 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.
  8. </details>
  9. # 答案1
  10. **得分**: 0
  11. 请尝试这样做:
  12. ```python
  13. driver.find_element_by_xpath('//button[@type="submit"]/span[1]').click()
  14. ```
  15. <details>
  16. <summary>英文:</summary>
  17. Try this:
  18. driver.find_element_by_xpath(&#39;//button[@type=&quot;submit&quot;]/span[1]&#39;).click()
  19. </details>
  20. # 答案2
  21. **得分**: 0
  22. 以下是翻译好的部分:
  23. **我确认我的订单** 文本位于 `&lt;button&gt;` 元素的子元素 `&lt;span&gt;` 中。因此,要对该元素执行 `click()` 操作,您可以使用以下任一[定位策略](https://stackoverflow.com/questions/48369043/official-locator-strategies-for-the-webdriver/48376890#48376890):
  24. - `cssSelector`
  25. driver.findElement(By.cssSelector("button.button.btn.btn-default.button-medium > span i.icon-chevron-right.right")).click();
  26. - `xpath`
  27. driver.findElement(By.xpath("//button[@class='button btn btn-default button-medium'][./span[contains(., '我确认我的订单')]]")).click();
  28. 然而,由于该元素是一个启用了[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):
  29. - `cssSelector`
  30. new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.button.btn.btn-default.button-medium > span i.icon-chevron-right.right"))).click();
  31. - `xpath`
  32. new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='button btn btn-default button-medium'][./span[contains(., '我确认我的订单')]]"))).click();
  33. <details>
  34. <summary>英文:</summary>
  35. 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):
  36. - `cssSelector`:
  37. driver.findElement(By.cssSelector(&quot;button.button.btn.btn-default.button-medium &gt; span i.icon-chevron-right.right&quot;)).click();
  38. - `xpath`:
  39. 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();
  40. 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):
  41. - `cssSelector`:
  42. 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();
  43. - `xpath`:
  44. 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();
  45. </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:

确定