Selenium找不到元素

2024-09-30 08:27:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我用selenium编写了一个代码来提取足球联赛中的回合数,所有元素对于我所能看到的所有页面都是相同的,但是由于某些原因,该代码对某些链接有效,而对其他链接无效。你知道吗

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)

这里是一个工作的链接Nowgoal Serie B,它返回带有类lsm2td标记的数量

以及源页面的图片enter image description here

这个返回值是0,由于某种原因,它找不到类为lsm2Nowgoal Serie A的标记,也找不到感兴趣的片段的图片 enter image description here 即使我试图用注释行a_ = driver.find_elements_by_class_name('lsm2')直接找到它,它仍然返回0。我会很感激你的帮助。你知道吗


Tags: 代码fromimportby链接driverselenium页面
1条回答
网友
1楼 · 发布于 2024-09-30 08:27:38

据我所知,带有“showRound”id的td的内部HTML是动态的,由showRound()JS函数加载,然后在页面加载时由页面的head标记中的脚本调用。因此,在您的情况下,似乎没有足够的时间来加载。我试着用两种方法来解决这个问题:

  1. 乱七八糟的:使用driver.implicit\u等待(等待的秒数)。我还建议将来使用它而不是sleep()。然而,这个解决方案非常笨拙,而且有点异步;换句话说,它主要等待秒倒计时,而不是结果。

  2. 我们可以等待加载第一个具有“lsm2”类的元素;如果在某个合理的超时之后它没有这样做,我们可以停止等待并引发en异常(感谢Zeinab Abbasimazar获得答案here)。这可以通过预期的\u条件WebDriverWait实现:

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_2;)而不是使用进行迭代,但这取决于您。你知道吗

相关问题 更多 >

    热门问题