python selenium 循环通过一些链接

Posted

技术标签:

【中文标题】python selenium 循环通过一些链接【英文标题】:python selenium loop through some links 【发布时间】:2021-08-21 02:49:51 【问题描述】:

我有一个链接数组,我试图访问每个链接并从中打印一些内容,然后返回主页并访问第二个链接,然后执行相同操作,直到完成数组中的所有链接。

发生的情况是第一个链接是唯一有效的链接,就像数组中的所有链接都消失了一样。我得到错误:

File "e:\work\MY CODE\scraping\learn.py", line 25, in theprint link.click()

    from selenium import webdriver
from selenium.webdriver.common import keys
#it make us able to use keybored keys like enter ,esc , etc....
from selenium.webdriver.common.keys import Keys
import time

#make us can wait for event to happen until run the next line of code
from selenium.webdriver.common.by import By
from selenium.webdriver.remote import command
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#get the google chrome driver path
PATH="E:\work\crom\chromedriver.exe"
#pass the pass to selenium webdriver method
driver=webdriver.Chrome(PATH)
#get the link of the site we want
driver.get("https://app.dealroom.co/companies.startups/f/client_focus/anyof_business/company_status/not_closed/company_type/not_government%20nonprofit/employees/anyof_2-10_11-50_51-200/has_website_url/anyof_yes/slug_locations/anyof_france?sort=-revenue")

#wait for the page to load
time.sleep(5)
#get the links i want to get info from
the_links=driver.find_elements_by_class_name("table-list-item")

#function that go the link and print somethin and return to main page
links=[]
the_links=driver.find_elements_by_class_name("table-list-item")
for link in the_links:
      links.append(link.get_attribute('href'))

for link in links:
      driver.get(link)
      website=driver.find_element_by_class_name("item-details-info__url")
      print(website.text)
      driver.back()
      time.sleep(3)
      

【问题讨论】:

您是否获得过时的元素引用?您不能定义一个元素,切换页面,然后再次使用该元素。看起来这就是您正在尝试做的事情,这会导致过时元素错误。' 是的,我知道了,你能告诉我另一种方法吗?? 【参考方案1】:

您的代码将抛出一个过时的元素引用错误,因为当您导航到下一页时,保存前一页任何元素的变量将变得不可用。

所以你需要做的是将所有元素存储在数组中,然后像这样循环遍历它:

links=[]
the_links=driver.find_elements_by_class_name("table-list-item")
for link in the_links:
    links.append(link.get_attribute('href'))

for link in links:
    driver.get(link)
    print("do something on this link")

或者您可以在当前使用 while 循环,然后在 driver.back() 再次填充 the_links 变量。

【讨论】:

好的,我试过这段代码,但是 get() 方法不起作用你知道为什么吗?我添加的内容:对于链接中的链接: driver.get(link) website=driver.find_element_by_class_name("item-details-info__url") print(website.text) driver.back() time.sleep(3) 如果 get 方法不起作用,那么您可以使用 javascript 执行器来加载新的 URL。例如,driver.execute_script("window.location.href = ".format(link))。这会将您带到新的网址。【参考方案2】:

Karim,class_name 为“item-details-info__url”的元素是否出现在所有页面上?另外,get() 方法会抛出什么错误?

【讨论】:

是的,该类存在于所有页面上,get() 方法根本不起作用,我在它存在的行中收到错误我编辑了代码现在你可以看到它

以上是关于python selenium 循环通过一些链接的主要内容,如果未能解决你的问题,请参考以下文章

Python+Selenium+Unittest编写超链接点击测试用例

使用Chrome驱动程序通过python和selenium在指定位置下载文件

在 Python 中使用 Selenium 滚动模式窗口

Python+Selenium练习篇之4-利用link text定位元素

如何通过 IE11 解决 selenium python 中嵌套 HTML 中的超链接?

AttributeError:'str'对象在尝试遍历hrefs并通过Selenium和Python单击它们时没有属性'click'