从csv文件中提取URL时,硒打开后不会向下滚动

时间:2018-09-25 15:50:14

标签: python selenium

因此,我要从csv中提取URL,我需要向下滚动直到其不再滚动为止。它将依次通过URL 1。不花时间停下来向下滚动。我尝试过time.sleep(),但是不起作用。

但是,如果我使用1个url(而不是从文件中提取它),它的工作原理就像冠军。

以下是一些网址供您参考。

我很感谢大家提出的任何建议。

new Notification("test");

以下是让Google驱动程序忽略Notification PopUps的代码

https://www.facebook.com/search/pages/?q=Los%20Angeles%20remodeling
https://www.facebook.com/search/pages/?q=Boston%20remodeling
https://www.facebook.com/search/pages/?q=Chicago%20remodeling



import time
from selenium import webdriver
from bs4 import BeautifulSoup as bs
import csv
import requests
from selenium.webdriver.support.ui import WebDriverWait



driver = webdriver.Firefox()

_browser_profile = webdriver.FirefoxProfile()
_browser_profile.set_preference("dom.webnotifications.enabled", False)
webdriver.Firefox(firefox_profile=_browser_profile)

driver.get('https://www.facebook.com')
username = driver.find_element_by_id("email")
password = driver.find_element_by_id("pass")
username.send_keys("email****")
password.send_keys("password****")
driver.find_element_by_id('loginbutton').click()
time.sleep(2)

with open('fb_urls.csv') as f_input, open('fb_profile_urls.csv', 'w', newline='')  as f_output:
    csv_input = csv.reader(f_input)
    csv_output = csv.writer(f_output)
    for url in csv_input:
        driver.get(url[0])
        WebDriverWait(driver, 10)
        lenOfPage = driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
        match=False
        while(match==False):
            lastCount = lenOfPage
            time.sleep(1)
            lenOfPage = driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
            if lastCount==lenOfPage:
                match=True
        soup = bs(driver.find_elements_by_class_name('_32mo'), "lxml")
        csv_output.writerow(soup)

1 个答案:

答案 0 :(得分:0)

我发现这与Firefox Webdriver有关。切换到Chrome即可使用。除了上面的代码,我还提供了让Google忽略Notification PopUp所需的正确代码。

相关问题