简体   繁体   English

Selenium Python:查找元素,其href属性具有必需的关键字

[英]Selenium Python : find element whose href attribute has required keyword

The page I'm working on is in this link . 我正在处理的页面在此链接中

This is the relevant portion of that page: 这是该页面的相关部分:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
  <head>...</head>
  <body>
    ...
    <div id="searchResults">
      <div class="box-related">...</div>
      <a href='downloadDataServlet?category=true&amp;type=epar' onclick=""><img src="/ema/images/icon_download_spread.gif" />Download results to spreadsheet</a>
      <div class="table-holder">
        <table class="table-epar eparResults" border="1" cellpadding="0" cellspacing="0" summary="Search results for EPARs ordered alphabetically">
          <caption>EPAR Search results</caption>
          <thead> ... </thead>
          <tbody>
            <tr>
              <th scope="row" class="key-detail name word-wrap">
                <a href="index.jsp?curl=pages/medicines/human/medicines/000471/human_med_000619.jsp&amp;mid=WC0b01ac058001d124">Abilify</a>
              </th>
              ...
            </tr>
            <tr>...</tr>
          </tbody>
        </table>
      </div>
    </div>
  </body>
</html>

This is the XPath location of the element I wish to select: 这是我希望选择的元素的XPath位置:

//*[@id="searchResults"]/div[2]/table/tbody/tr[1]/th/a

But there may be many results on the searchpage, so I want to click on the link whose URL has the product number that I'm searching for (which is 000471 in this case). 但是搜索页面上可能有很多结果,因此我想单击链接,该链接的URL包含我要搜索的产品编号(在本例中为000471 )。 I want to select the <a> element which contains that string in the href attribute. 我想选择在href属性中包含该字符串的<a>元素。

Here's what I've tried: 这是我尝试过的:

inp = driver.find_element_by_xpath("//*[@id='searchResults']/div[2]/table/tbody/tr[1]/th/a[contains(@href,'"+str3+"')]")
inp.click()

where str3 has the value 000471 in this case. 在这种情况下,其中str3的值为000471 But I keep getting NoSuchElementException . 但是我不断收到NoSuchElementException

Any help would be appreciated! 任何帮助,将不胜感激!

The problem is probably cause by elements which are inserted into the source code viewer or inspector when rebuilding the table. 问题可能是由重建表时插入到源代码查看器或检查器中的元素引起的。 The tbody tag is usually inserted in the code when it doesn't actually exist in the real source. tbody标签在实际源代码中实际上不存在时,通常会插入到代码中。

You can eliminate the unnecessary steps in your XPath, if you can still obtain a unique location path to the data you wish to select. 如果仍可以获取要选择的数据的唯一位置路径,则可以消除XPath中不必要的步骤。 This might be sufficient: 这可能就足够了:

//*[@id='searchResults']//a[contains(@href,'000471')]

If the other steps are still necessary, you can try it without the tbody . 如果仍然需要其他步骤,则可以不用 tbody尝试。

Update I also noticed that your search page declares a namespace: 更新我还注意到您的搜索页面声明了一个名称空间:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
    ...

Automatic registration of default namespaces is implementation dependent. 默认名称空间的自动注册取决于实现。 XPath requires all selectors to be qualified with a namespace. XPath要求所有选择器都必须具有名称空间。 If your selenium implementation doesn't do that, you need to either register a namespace/prefix mapping, and prefix all elements in the namespace (ex: //h:table/h:tr/h:td ) or ignore the namespace, using wildcards and comparing the local name in a predicate. 如果您的selenium实现不这样做,则需要注册一个名称空间/前缀映射,并为该名称空间中的所有元素添加前缀(例如: //h:table/h:tr/h:td )或忽略该名称空间,使用通配符并在谓词中比较本地名称。

If the namespace is keeping you from selecting the node, you can ignore it with this expression: 如果名称空间使您无法选择节点,则可以使用以下表达式忽略它:

//*[@id='searchResults']//*[local-name() = 'a'][contains(@href,'000471')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Python在Selenium中的元素中查找所有href - How to find all href in an element in Selenium with Python 如何使用 Python Selenium 单击具有 href 属性的元素 - How to click the element with href attribute using Python Selenium 如何使用 Selenium 和 Python 提取元素的 href 属性 - How to extract the href attribute of an element using Selenium and Python 如何使用 selenium 和 python 单击包含 href 属性的元素 - How to click on element including href attribute using selenium and python 如何使用 Selenium 和 Python 获取元素的 href 属性 - How to Get the href attribute of the element using Selenium and Python 如何通过硒和python查找抽搐视频的href属性? - How to find the href attribute of the videos on twitch through selenium and python? python硒查找元素,其属性包含某些文本 - python selenium find element with attribute containing sometext 如何找到具有href值某些部分的元素? (python硒) - How to find an element with some parts of href value ? (python selenium) 如何使用 selenium python 通过 href 值查找元素? - How to find an element by href value using selenium python? Selenium - Python - AttributeError: 'WebDriver' object 没有属性 'find_element_by_name' - Selenium - Python - AttributeError: 'WebDriver' object has no attribute 'find_element_by_name'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM