【问题标题】:Trying to find the correct xpath试图找到正确的 xpath
【发布时间】:2022-01-21 11:19:00
【问题描述】:

我已经做了代码,看下面几行:

from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import time
    from selenium.webdriver.common.by import By

    PATH = "C:\Program Files (x86)\chromedriver.exe"
    driver = webdriver.Chrome(PATH)

    driver.get("https://www.flashscore.com/match/jBvNMej6/#match-summary")
    print(driver.title)
    driver.maximize_window() # For maximizing window
    driver.implicitly_wait(10) # gives an implicit wait for 20 seconds

    driver.find_element_by_id('onetrust-reject-all-handler').click()
    time.sleep(2)
    driver.find_element(By.CLASS_NAME,'previewShowMore.showMore').click()

    main = driver.find_element(By.CLASS_NAME,'previewLine'[b[text()="Hot stat:"]]/text)
    print(main.text)

    time.sleep(2)
    driver.close()

但是,我收到以下错误。

main = driver.find_element(By.CLASS_NAME,'previewLine'[b[text()="Hot stat:"]]/text) ^ SyntaxError: 无效语法

我能做些什么来避免这种情况?

谢谢! :)

【问题讨论】:

    标签: selenium web-scraping xpath


    【解决方案1】:

    嗯,在这一行

    main = driver.find_element(By.CLASS_NAME,'previewLine'[b[text()="Hot stat:"]]/text)
    

    你做了一个很棒的组合:)
    您的定位器绝对无效。
    此外,如果您想打印没有“热点”的段落文本,则需要从整个 div(段落)文本中删除该字符串。
    这应该可以实现您想要实现的目标:

    main = driver.find_element(By.XPATH,"//div[@class='previewLine' and ./b[text()='Hot streak']]").text
    main = main.replace('Hot streak','')
    print(main)
    

    【讨论】:

      【解决方案2】:

      我没有找到任何文本“热门统计:”。您必须在找到的地方附上 html 代码。

      我假设您要检索特定 previewLine 的文本?

      main = driver.find_element(By.XPATH ,'//div[@class="previewLine"]/b[contains(text(),"Hot streak")]/..')
      print(main.text)
      

      【讨论】:

      • 没错。如果我没记错的话,我想打印的行是名为“Hot stat:”的 previewLine。
      猜你喜欢
      • 1970-01-01
      • 2019-11-15
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多