【问题标题】:finding textarea element with xpath使用 xpath 查找 textarea 元素
【发布时间】:2020-06-07 02:54:35
【问题描述】:

我一直在为这个 selenium 项目而苦苦挣扎,并一直在寻找与这个问题无关的旧参考资料或帖子。我承认我是 xpath 和 selenium 的新手,所以希望它可能像修复我的语法一样简单。如果这有什么不同,我在 lubuntu 19.10 上使用 python 3.6.9。

我正在尝试访问的html元素:

 <div class="NfvXc">
   <textarea aria-label="Write a caption…" autocomplete="off" autocorrect="off"
   class="_472V_" placeholder="Write a caption…"></textarea>
  </div>

我试过的代码:

add_text = browser_object.find_element_by_xpath("//textarea[contains(@aria-label,'Write a caption...')]")

add_text = browser_object.find_element_by_xpath("//div[contains(text(),'Write a caption...')]")

我在尝试不同的 xpath 时得到的错误消息:

error in process_image(): Message: Given xpath expression "//*div[contains(text(),'Write a caption...')]" is invalid: SyntaxError: The expression is not a legal expression.

我检查了 firefox 以尝试捕获 xpath,显示为: /html/body/div[1]/section/div[2]/section[1]/div[1]/textarea

虽然我不确定如何调整我的 xpath 以找到该元素,但显然。

【问题讨论】:

  • 您正在使用//*div[contains(@placeholder,'Write a caption...')],请使用//div[contains(@placeholder,'Write a caption...')],或者如果您确定页面中没有其他元素包含占位符Write a caption...//*[contains(@placeholder,'Write a caption...')],请使用它

标签: html python-3.x selenium-webdriver xpath


【解决方案1】:

错误消息//*div[contains(text(),'Write a caption...')] 中的XPath 无效,因为您不能同时拥有* 和div 以及XPath 的开头。选择其中之一,例如//div[contains(text(),'Write a caption...')]
但是它仍然无法工作,因为文本位于属性中,您可以使用 //textarea[@aria-label='Write a caption…']
请注意,//textarea[contains(text(),'Write a caption…')] 将不返回匹配项。 您可以在浏览器中使用控制台中的语法 $x() 测试您的 XPath,更多信息 here

【讨论】:

  • 感谢您的澄清和回答!我还想指出我不清楚的是,这三个时期实际上是一个椭圆,显然不匹配。
猜你喜欢
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 2015-11-26
  • 1970-01-01
  • 1970-01-01
  • 2020-05-02
  • 1970-01-01
  • 2011-04-14
相关资源
最近更新 更多