简体   繁体   English

无法在python中使用硒找到链接?

[英]Unable to find links using selenium in python?

I am scraping a webpage using selenium. 我正在使用硒抓取网页。 I first find the link I want and then click on it and download it(Link is pdf). 我首先找到我想要的链接,然后单击并下载(链接为pdf)。 What happens is sometimes I am able to do so, but sometimes selenium says that link not found. 发生的事情有时是我能够做到的,但硒表示有时找不到该链接。 I suppose that it is due to the page not loading properly. 我想这是由于页面无法正确加载。 What can I do about this and am I in the right direction? 我该怎么办,我的方向正确吗?

This is my previous code: 这是我以前的代码:

for b in source_code_2.find_all('a', href=True):
    if b.has_attr("title"):
        if(b['title']=='Click here to download'):
            urllib2.urlretrieve(full_url)

now i want to do it using selenium and element. 现在我想用硒和元素。 How can I do this? 我怎样才能做到这一点?

I think you should use explicit wait to tell selenium to wait until specific element loads properly , In python you can use explicit wait in following way : 我认为您应该使用显式等待告诉selenium等待直到特定元素正确加载,在python中,您可以通过以下方式使用显式等待:

  element = WebDriverWait(driver, 20).until(
    EC.presence_of_element_located((By.ID, "yourElement"))

OR 要么

element = WebDriverWait(driver, 20).until(
    EC.element_to_be_clickable((By.ID, "yourElement"))
    element.click()

You just need to replace your element ID in above code and you can change 20 seconds to 30 ,40 as per your need. 您只需要在上面的代码中替换元素ID,即可根据需要将20秒更改为30,40。 So meaning of above code is your webdriver will wait until 20 seconds to find that specific element. 因此,上述代码的意思是您的网络驱动程序将等待20秒才能找到该特定元素。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM