硒找不到元素Python

我用硒编写了一个代码,以提取足球联赛中的回合数,从我所看到的所有页面来看,所有元素都是相同的,但是由于某些原因,该代码适用于某些链接,而不适用于其他链接。

function overlayClick() {
  alert('overlayClick');
}

这里是一个有效的链接Nowgoal Serie B,它返回类from selenium import webdriver from selenium.webdriver.firefox.options import Options from time import sleep def pack_links(l): options = Options() options.headless = True driver = webdriver.Chrome() driver.get(l) rnds = driver.find_element_by_id('showRound') a_ = rnds.find_elements_by_xpath(".//td[@class='lsm2']") #a_ = driver.find_elements_by_class_name('lsm2') knt = 0 for _ in a_: knt = knt+1 print(knt) sleep(2) driver.close() return None link = 'http://info.nowgoal.com/en/League/34.html' pack_links(link) 的{​​{1}}标签的数量

以及源页面的外观

硒找不到元素Python

此返回值为0,由于某种原因,它找不到类别为td Nowgoal Serie A的标记,也找不到感兴趣的段的图片

硒找不到元素Python

即使当我尝试直接在此注释行lsm2中查找它时,它仍然返回0。对此,我将不胜感激。

a55713766 回答:硒找不到元素Python

据我了解,带有“ showRound” id的 td 的内部HTML是动态的,并由 showRound() JS函数加载,该JS函数又被调用按页面加载时页面 head 标记中的脚本。因此,在您的情况下,似乎没有足够的时间来加载。我试图通过两种方式解决此问题:

  1. 一个讨厌的人:使用 driver.implicitly_wait(number_of_seconds_to_wait)。我也建议将来使用它代替 sleep()。但是,这种解决方案非常笨拙,并且是异步的。换句话说,它主要等待秒倒数而不是结果。

  2. 我们可以等待第一个具有“ lsm2”类的元素加载;如果在合理的超时后仍未这样做,我们可能会停止等待并提出例外要求(感谢 Zeinab Abbasimazar 来回答here)。这可以通过 expected_conditions WebDriverWait

  3. 来实现。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

def pack_links(l):
    options = webdriver.ChromeOptions()  # I would also suggest to use this instead of Options()
    options.add_argument("--headless")
    options.add_argument("--enable-javascript")  # To be on the safe side,although it seems to be enabled by default
    driver = webdriver.Chrome("path_to_chromedriver_binary",options=options)
    driver.get(l)
    rnds = driver.find_element_by_id('showRound')

    """Until now,your code has gone almost unchanged. Now let's wait for the first td element with lsm2 class to load,with setting maximum timeout of 5 seconds:"""

    try:
        WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CLASS_NAME,"lsm2")))
        print("All necessary tables have been loaded successfully")
    except TimeoutException:
        raise("Timeout error")


    """Then we proceed in case of success:"""

    a_ = rnds.find_elements_by_xpath(".//td[@class='lsm2']")
    knt = 0
    for _ in a_:
        knt = knt+1

    print(knt)

    driver.implicitly_wait(2)  # Not sure if it is needed here anymore
    driver.close()
    driver.quit()  # I would also recommend to make sure you quit the driver not only close it if you don't want to kill numerous RAM-greedy Chrome processes by hand 
    return None

您可以进行一些实验并调整超时时间,以达到所需的结果。我也建议使用 len(a _)而不是使用 for 循环进行迭代,但这取决于您。

本文链接:https://www.f2er.com/3084696.html

大家都在问