简体   繁体   English

如何使用Selenium Geckodriver收集数据python

[英]How to collect data python using selenium geckodriver

I tried to scrap data from website using selenium firefox and geckodriver. 我尝试使用Selenium firefox和geckodriver从网站上抓取数据。 I want to navigate from one page to another and extract informations. 我想从一页导航到另一页并提取信息。 I am unable to move from current page to another, and force driver to back to the original page and move to next element in the list. 我无法从当前页面移动到另一个页面,并迫使驱动程序返回到原始页面并移动到列表中的下一个元素。 I created the code bellow which click on first element and go to the specific page to collect data. 我创建了以下代码,单击第一个元素,然后转到特定页面以收集数据。 Thank you 谢谢

   binary = FirefoxBinary('/usr/bin/firefox')
    driver = webdriver.Firefox(firefox_binary=binary, executable_path=r'/home/twitter/geckodriver')
    try:
        driver.get('https://www..........')  
        list_element = driver.find_elements_by_xpath("//span[@class='icon icon-email']")
for element in list_element :         
        x = driver.current_url
        element.click()
        time.sleep(5)
        for ex in driver.find_elements_by_xpath('//span[@class = "valeur"]'):
            print (ex.text)
        driver.back()
except Exception as e:
         print (e)
driver.quit()

This might happens because your driver/browser didn't get the new page (current page). 这可能是因为您的驱动程序/浏览器未获取新页面(当前页面)。 add one line after element.click() or time.sleep(5) : element.click()time.sleep(5)之后添加一行:

driver.switch_to.window(driver.current_window_handle)

then try to run your code again. 然后尝试再次运行您的代码。 Hope this helps you! 希望这对您有所帮助! :) :)

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

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