Selenium 无头浏览不起作用?

Selenium browsing with headless isn't working?

我正在尝试将 Selenium 与无头浏览器一起使用,但是当我 运行 下面的这段代码仍然打开浏览器时?

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.keys import Keys


def address_One():
    page_Number = 1
    while page_Number <= 15:
        with open("/home/admin-acc/Desktop/user-information.txt", mode="a") as writer:
            url = f"https://advanced.name/freeproxy?page={page_Number}"
            options = webdriver.FirefoxOptions()
            options.add_argument("--headless")
            driver = webdriver.Firefox(options=options)
            driver.get(url)
            all_ips = driver.find_elements_by_xpath('//td[@data-ip]')
            all_ports = driver.find_elements_by_xpath('//td[@data-port]')
        for ip, port in zip(all_ips, all_ports):
            print(ip.text, port.text)
        driver.quit()
        page_Number += 1


if __name__ == '__main__':
    address_One()

我做了以下 更改,它似乎工作正常:

说明 : 在 IF 子句中实例化驱动程序,然后将其传递给 address_One,如下所示:

def address_One(driver):
    page_Number = 1
    while page_Number <= 15:
        with open("/home/admin-acc/Desktop/user-information.txt", mode="a") as writer:
            url = f"https://advanced.name/freeproxy?page={page_Number}"
            driver.get(url)
            all_ips = driver.find_elements_by_xpath('//td[@data-ip]')
            all_ports = driver.find_elements_by_xpath('//td[@data-port]')
        for ip, port in zip(all_ips, all_ports):
            print(ip.text, port.text)
        driver.quit()
        page_Number += 1

if __name__ == '__main__':
    executablePath = r'C:\geckodriver.exe'
    options = webdriver.FirefoxOptions()
    options.add_argument("--headless")
    driver = webdriver.Firefox(executable_path = executablePath, options=options)
    address_One(driver)

输出:

91.137.238.54 8082
190.83.125.15 999
143.255.52.102 31158
41.180.68.195 8080
187.60.121.246 3128
138.122.99.102 999
200.199.143.162 8080
45.5.117.250 999
129.205.127.30 8080
177.47.181.250 8080
190.131.229.133 999
45.160.78.5 999
190.121.13.140 999
103.52.211.126 80
143.55.57.18 8080
116.90.229.186 35561
45.189.252.42 999
45.235.216.112 8080
103.137.199.129 8080
103.103.212.222 53281
40.73.42.49 3128
170.231.187.209 8080
187.92.132.14 8080
27.123.1.46 3128
36.89.51.165 8080
102.164.248.157 8080
45.234.16.194 8080
103.243.82.198 37358
8.210.88.234 3128
49.0.41.81 38235
103.101.233.13 8080
103.94.164.16 8088
103.154.65.218 8080
202.152.12.202 8080
45.236.103.33 8085
190.52.198.180 8080
178.32.129.31 3128
110.78.81.107 8080
45.236.103.130 8085
103.122.64.212 8080
103.52.211.138 80
170.83.79.105 999
104.238.195.10 80
177.70.243.41 8080
176.32.185.22 8080
190.83.125.17 999
177.92.0.2 8080
89.20.48.118 8080
52.151.15.4 80
45.251.231.78 8080
103.110.10.206 8080
103.198.126.237 8888
59.124.224.180 3128
178.18.255.175 3128
45.234.5.197 6666
45.174.248.4 999
195.154.67.61 3128
51.75.144.32 3128
103.146.151.58 8080
49.75.59.242 3128
190.108.200.176 8080
45.189.252.9 999
183.88.192.112 8080