【问题标题】:How to find element using xpath Python selenium如何使用 xpath Python selenium 查找元素
【发布时间】:2019-11-30 11:27:26
【问题描述】:

由于某种原因,我在使用 xpath 时无法访问数字和标题。

这是 HTML:

<div class="style-scope classification-tree">
            <state-modifier class="code style-scope classification-tree" act="{&quot;type&quot;: &quot;QUERY_ADD_CPC&quot;, &quot;cpc&quot;: &quot;$cpc&quot;}" first="true" data-cpc="C07C311/51">
                  <a id="link" href="/?q=C07C311%2f51" class="style-scope state-modifier">C07C311/51</a>
            </state-modifier>
            <span class="description style-scope classification-tree">Y being a hydrogen or a carbon atom</span>
          </div>

到目前为止,我已经尝试过这段代码:

Class_Content_year = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//state-modifier[@class='code style-scope classification-tree']//a[contains(@id, 'link') and contains(@class, 'style-scope state-modifier')]"))).text

Class_Content_title = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//div[@class='style-scope classification-tree']//span[contains(@class, 'description style-scope classification-tree')]"))).text

它应该得到里面的文字和。

但是,发生了这个错误:

Traceback (most recent call last):
  File "<ipython-input-2-dfe4f1a9b070>", line 97, in openURL
    Class_Content = Class(driver, Current_Content)
  File "c:\Users\jyg\Desktop\MT\Extract_data_2.py", line 57, in Class
    Class_Content_year = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//div[@class='style-scope classification-tree']//state-modifier[contains(@class, 'code style-scope classification-tree']/child::a[contains(@id, 'link') and contains(@class, 'style-scope state-modifier')]"))).text
  File "C:\Users\jyg\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

有人可以帮忙吗?谢谢!

【问题讨论】:

  • 你可以试试这个:Class_Content_year = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.ID, "link"))).text Class_Content_title = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//span[@class='description style-scope classification-tree')]"))).text
  • 如果您看到错误是在不同的xpath 而不是您发布的xpath 上抛出错误。
  • 您正在将 WebDriverWait 和 element.text 组合在一起。 WebDriverWait 什么也不返回。将这些作为单独的步骤尝试可能是个好主意。
  • id='link' 位于页面的多个部分,因此仅查找是不够的。我试图合并两个属性,但它不起作用。

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

这是要使用的 xpath。

代码:

Class_Content_year = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//div[@class='style-scope classification-tree']//state-modifier[contains(@class, 'code style-scope classification-tree')]//a[contains(@id, 'link') and contains(@class, 'style-scope state-modifier')]")))
 # now get the text
 print(Class_Content_year)
 # now get the text from span
 print(driver.find_element_by_xpath("//div[@class='style-scope classification-tree']//span[@class='description style-scope classification-tree']").text)

以下是其他可能的 xpath:

//div[@class='style-scope classification-tree']//a[@class='style-scope state-modifier']

对于 span,您可以使用以下 xpath。

//div[@class='style-scope classification-tree']//span[@class='description style-scope classification-tree']

【讨论】:

    【解决方案2】:

    要提取文本 C07C311/51 而不是使用 presence_of_element_located(),您需要使用 visibility_of_element_located() 并且可以使用以下任一 Locator Strategy

    • 使用XPATH

      driver.get("https://patents.google.com/patent/JP2009517369A/en?oq=JP2009517369]")
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='style-scope classification-tree' and not(@hidden)]/state-modifier[@class='code style-scope classification-tree']/a[@class='style-scope state-modifier']"))).get_attribute("innerHTML"))
      
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      

    【讨论】:

    • 不幸的是它没有工作。恐怕它与动态元素有关。请参阅“分类”下的patents.google.com/patent/JP2009517369A/en?oq=JP2009517369。我尝试在这里获取代码。
    • @JenG 查看更新的答案并告诉我状态。
    • 感谢您的编辑!但是,它仍然进入 timeoutException .. 我认为它与隐藏元素有关,但似乎仍然找不到它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多