如何找到包含单词“下载”的所有元素使用硒x路径? [英] How to find all elements containing the word "download" using Selenium x-path?

查看:88
本文介绍了如何找到包含单词“下载”的所有元素使用硒x路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Selenium进行一些网页浏览,现在我想查找用户可以点击的所有元素,并在链接文本,按钮文本,按钮文本中包含单词下载(任何大小写)元素 id ,元素 class 或者 href 。这可以包括链接,按钮或任何其他元素。



此答案我找到了一个用于寻找xpath的xpath,以便根据特定的文本(或者非区分大小写和局部匹配)搜索按钮:

 text ='download'
driver.find_elements_by_xpath((// * [contains(text(),'download')])

但在此页面返回任何结果,即使在这里有以下链接:

 < a id =downloadTopclass =navlinkhref =jav ascript:__ doPostBack( downloadTop,)>下载及LT; / A> 

有人知道我怎么能找到所有包含单词下载的元素在网站中?




这个问题被标记为一个重复的问题,它得到答案,其中建议将其更改为// * [text()[contains(。,'download')]]。所以我尝试了以下方法:

 >>> from selenium import webdriver 
>>> d = webdriver.Firefox()
>>>链接=https://www.yourticketprovider.nl/LiveContent/tickets.aspx?x=492449&y=8687&px=92AD8EAA22C9223FBCA3102EE0AE2899510C03E398A8A08A222AFDACEBFF8BA95D656F01FB04A1437669EC46E93AB5776A33951830BBA97DD94DB1729BF42D76&rand=a17cafc7-26fe-42d9-a61a-894b43a28046&utm_source=PurchaseSuccess&utm_medium=Email& ; utm_campaign = SystemMails'
>>> d.get(link)
>>> d.find_elements_by_xpath(// * [text()[contains(。,'download')]])
[]#你可以看到它仍然没有得到任何结果..
>>>

有人知道我如何获取用户可以点击的所有元素,哪些元素包含单词下载链接文本,按钮文本,元素 id ,元素 class HREF ?我们欢迎您提供所有提示!

解决方案

$ b

  // * [(@ id | @class | @href | text())
[contains(translate(。,'DOWNLOAD','download' ),'download')]]

此Xpath 1.0表达式选择:所有具有 id class href 属性的元素或文本节点的孩子,其字符串值包含字符串下载:在任何大小写。

这是一个运行证明。XSLT转换下面的代码用于评估XPath表达式并将所有选定的节点复制到输出:

 < xsl:stylesheet version = 1.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform> 
< xsl:output omit-xml-declaration =yesindent =yes/> ;

< xsl:template match =/>
< xsl:copy-of select =
// * [(@ id | @class | @href | text())
[包含(翻译(。,'下载','下载'),'下载')]]
/>
< / xsl:template>
< / xsl:stylesheet>

当我们将转换应用于以下测试文档时:

 < html> 
href =javascript:__ doPostBack('downloadTop','')>下载< / a>
< b id =yclass =x_downLoad/>
< p> Nothing to do_wnLoad< / p>
< b> dOwnlOad< / b>
< / html>

选中需要的元素,然后复制到输出 p>

 < a id =downloadTopclass =navlinkhref =javascript:__ doPostBack('downloadTop','') >下载和LT; / A> 
< b id =yclass =x_downLoad/>
< b> dOwnlOad< / b>


I'm using Selenium to do some webscraping and I now want to find all elements on which the user can click and which contain the word "download" (in any capitalization) in either the link text, the button text, the element id, the element class or the href. This can include both links, buttons or any other element.

In this answer I found the an xpath for somebody looking for an xpath to search for buttons based on a certain text (or non-case-sensitive and partial matches):

text = 'download'
driver.find_elements_by_xpath("(//*[contains(text(), 'download')]")

but on this page that returns no results, even though the following link is in there:

<a id="downloadTop" class="navlink" href="javascript:__doPostBack('downloadTop','')">Download</a>

Does anybody know how I can find all elements which somehow contain the word "download" in a website?

[EDIT] This question was marked as a duplicate for a question which gets an answer in which it is suggested to change it to "//*[text()[contains(.,'download')]]". So I tried the following:

>>> from selenium import webdriver
>>> d = webdriver.Firefox()
>>> link = 'https://www.yourticketprovider.nl/LiveContent/tickets.aspx?x=492449&y=8687&px=92AD8EAA22C9223FBCA3102EE0AE2899510C03E398A8A08A222AFDACEBFF8BA95D656F01FB04A1437669EC46E93AB5776A33951830BBA97DD94DB1729BF42D76&rand=a17cafc7-26fe-42d9-a61a-894b43a28046&utm_source=PurchaseSuccess&utm_medium=Email&utm_campaign=SystemMails'
>>> d.get(link)
>>> d.find_elements_by_xpath("//*[text()[contains(.,'download')]]")
[]  # As you can see it still doesn't get any results..
>>>

Does anybody know how I can get all elements on which the user can click and which contain the word "download" in either the link text, the button text, the element id, the element class or the href? All tips are welcome!

解决方案

Try this:

//*[(@id|@class|@href|text())
       [contains(translate(.,'DOWNLOAD','download'), 'download')]]

This Xpath 1.0 expression selects: all elements that have an id or class or href attribute or text-node child, whose string value contains the string "download: in any capitalization.

Here is a running proof. The XSLT transformation below is used to evaluate the XPath expression and to copy all selected nodes to the output:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="/">
    <xsl:copy-of select=
    "//*[(@id|@class|@href|text())
       [contains(translate(.,'DOWNLOAD','download'), 'download')]]
    "/>
  </xsl:template>
</xsl:stylesheet>

When we apply the transformation to the following test-document:

<html>
  <a id="downloadTop" class="navlink" 
    href="javascript:__doPostBack('downloadTop','')">Download</a>
  <b id="y" class="x_downLoad"/>
  <p>Nothing to do_wnLoad</p>
  <a class="m" href="www.DownLoad.com">Get it!</a>
  <b>dOwnlOad</b>
</html>

The wanted elements are selected and then copied to the output:

<a id="downloadTop" class="navlink" href="javascript:__doPostBack('downloadTop','')">Download</a>
<b id="y" class="x_downLoad"/>
<a class="m" href="www.DownLoad.com">Get it!</a>
<b>dOwnlOad</b>

这篇关于如何找到包含单词“下载”的所有元素使用硒x路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆