GVKun编程网logo

在Selenium测试中使用XPath通过文本获取WebElement

15

想了解在Selenium测试中使用XPath通过文本获取WebElement的新动态吗?本文将为您提供详细的信息,此外,我们还将为您介绍关于findElement的Seleniumxpath语法错误、

想了解在Selenium测试中使用XPath通过文本获取WebElement的新动态吗?本文将为您提供详细的信息,此外,我们还将为您介绍关于findElement 的 Selenium xpath 语法错误、python – WebElement上的Selenium WebDriver“find_element_by_xpath”、Python以及如何从Selenium元素WebElement对象获取文本?、Selenium WebDriver findElement(By.xpath())对我不起作用的新知识。

本文目录一览:

在Selenium测试中使用XPath通过文本获取WebElement

在Selenium测试中使用XPath通过文本获取WebElement

我想找到使用XPath基于文本的任何WebElement。

基本上,我要通过文本检索的WebElement包含一个输入元素。

我目前正在使用,

driver.findElement(By.xpath("//*[normalize-space(text()) = ''Own Hotel'']"));

找不到上面的WebElement,但通常可以检索所有
其他Web元素。

甚至,

By.xpath("//*[contains(text(),''Own Hotel'')]")

没有给我任何结果。虽然我对精确的文字匹配感兴趣。

我正在寻找一种通过文本来查找Web元素的方法,该文本
对Web元素内部存在的元素不重要。如果文本匹配,则应返回
WebElement。

答案1

小编典典

似乎文本被包装在标签内而不是输入。试试这个

driver.findElement(By.xpath(".//label[text()[normalize-space() = ''Own Hotel'']]"));

findElement 的 Selenium xpath 语法错误

findElement 的 Selenium xpath 语法错误

如何解决findElement 的 Selenium xpath 语法错误?

我刚刚开始使用 Selenium 进行自动化测试,并且一直在通过 udemy 学习教程。

一旦用户登录,我就找到了导航栏的 xpath,以确保该人已登录。在控制台中对其进行了测试,它可以找到导航栏。在我的脚本中设置它,它返回一个语法错误。

这是我试图定位的导航栏的 html:

<ui-viewhttps://www.jb51.cc/tag/cop/" target="_blank">cope">
  <app-route >
    <div>
      <app-headerhttps://www.jb51.cc/tag/cop/" target="_blank">cope">
        <header>
          <nav>

我的 xpath 代码:

WebElement navBar = driver.findElement(By.xpath("//ui-view[@ng-scope'']/app-route/div[@AppWrapper]/app-header[@ng-isolate-scope'']//nav[@NavBar'']"));

错误:

[ERROR] Tests run: 1,Failures: 1,Errors: 0,Skipped: 0,Time elapsed: 13.796 s <<< FAILURE! - in com.testing.autoTest
[ERROR] com.testing.autoTest.loginTest  Time elapsed: 8.387 s  <<< FAILURE!
org.openqa.selenium.InvalidSelectorException: 
invalid selector: Unable to locate an element with the xpath expression //ui-view[@ng-scope'']/app-route/div[@AppWrapper]/app-header[@ng-isolate-scope'']//nav[@NavBar''] because of the following error:
SyntaxError: Failed to execute ''evaluate'' on ''Document'': The string ''//ui-view[@ng-scope'']/app-route/div[@AppWrapper]/app-header[@ng-isolate-scope'']//nav[@NavBar'']'' is not a valid XPath expression.

控制台测试:

$x("//ui-view[@ng-scope'']/app-route/div[@AppWrapper'']/app-header[@ng-isolate-scope'']//nav[@NavBar'']")
[nav.NavBar]0: nav.NavBarlength: 1__proto__: Array(0)

我已将 xpath 分解为以 //ui-view[@ng-scope''] 开头的增量块,并逐渐向其中添加更多块,似乎一旦到达 //ui-view[@ng-scope'']/app-route/div[@AppWrapper],测试就会失败。

我也尝试过在页面上使用其他元素,但它在同一点中断。

如有任何提示,将不胜感激,谢谢。

编辑: zx485 指出的错误导致新错误:

[ERROR] com.testing.autoTest.loginTest  Time elapsed: 9.271 s  <<< FAILURE!
org.openqa.selenium.NoSuchElementException: 
no such element: Unable to locate element: {"method":"xpath","selector":"//ui-view[@ng-scope'']/app-route/div[@AppWrapper'']/app-header[@ng-isolate-scope'']//nav[@NavBar'']"}

编辑 2:我发现了第二个错误的问题,测试试图在元素加载之前运行得太快,因此在查找导航栏之前添加了一个 sleep(3000); 计时器并且测试通过了

解决方法

如评论中所述,您有一个类型 - 在 '' 中缺少结束 div[@AppWrapper]
但是我建议使用这个定位器:

//nav[@NavBar'']

这应该足够独特。

python – WebElement上的Selenium WebDriver“find_element_by_xpath”

python – WebElement上的Selenium WebDriver“find_element_by_xpath”

我正在尝试使用以下行查找元素:

elements = driver.find_elements_by_xpath("//div[@]")

一旦我有了元素,我知道有两个“显示”,我希望能够使用第二个,并在其中找到一个特定的元素,如下所示:

title = elements[1].find_element_by_xpath("//div[@]")

但是,它总是恢复使用第一个.我已经介入了它,它正在为“显示”找到2个元素,所以我不确定我做错了什么.

任何帮助将不胜感激.

最佳答案
我想你想要这个:

elements = driver.find_elements_by_xpath("//div[@]")
title = elements[1].find_elements_by_xpath(".//div[@]")

Python以及如何从Selenium元素WebElement对象获取文本?

Python以及如何从Selenium元素WebElement对象获取文本?

如何解决Python以及如何从Selenium元素WebElement对象获取文本??

一旦找到元素,就可以使用文本方法。

例:

for element in self.driver.find_elements_by_tag_name(''img''):
       print element.text
       print element.tag_name
       print element.parent
       print element.location
       print element.size

解决方法

我正在尝试通过使用Selenium方法来获取html页面中的标记文本内容,但似乎该方法someElement.getText()在Python中不可用。有什么帮助吗?

追溯:

AttributeError: ''WebElement'' object has no attribute ''getText''

Selenium WebDriver findElement(By.xpath())对我不起作用

Selenium WebDriver findElement(By.xpath())对我不起作用

我已经看过xpath教程并检查了许多其他帖子,因此我不确定自己缺少什么。我只是想通过xpath查找以下元素:

<inputtype="email" test-id="test-username"/>

我已经尝试了很多事情,例如:

element = findElement(By.xpath("//[@test-id='test-username']"));

错误是Expression is not a legal expression

我在MacBook上使用Firefox

任何建议将不胜感激。

关于在Selenium测试中使用XPath通过文本获取WebElement的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于findElement 的 Selenium xpath 语法错误、python – WebElement上的Selenium WebDriver“find_element_by_xpath”、Python以及如何从Selenium元素WebElement对象获取文本?、Selenium WebDriver findElement(By.xpath())对我不起作用等相关知识的信息别忘了在本站进行查找喔。

本文标签: