gpt4 book ai didi

python - 配置 Selenium Firefox 代理设置

转载 作者:行者123 更新时间:2023-12-01 02:46:20 26 4
gpt4 key购买 nike

我已经尝试了大约 2 天将 selenium 与代理服务器一起使用,但仍然无法做到。我已经尝试了selenium网站上的方法,复制并粘贴,但它不起作用。我已经尝试了 Stackoverflow 的所有答案,但没有一个起作用,我得到的最接近的是加载页面,但我的 IP 保持不变。

这是我最近尝试过的代码:

#!/usr/bin/env python

from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities



HOST = "144.217.31.225"
PORT = "3128"
def my_proxy(PROXY_HOST,PROXY_PORT):
fp = webdriver.FirefoxProfile()
# Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5
print PROXY_PORT
print PROXY_HOST
fp.set_preference("network.proxy.type", 1)
fp.set_preference("network.proxy.http",PROXY_HOST)
fp.set_preference("network.proxy.http_port",int(PROXY_PORT))
fp.set_preference("general.useragent.override","whater_useragent")
fp.update_preferences()
return webdriver.Firefox(firefox_profile=fp)

browser = my_proxy(HOST,PORT)
browser.get('https://www.google.com')

search=browser.find_element_by_xpath("//input[@title='Search']")
search.send_keys('my ip')

这是我收到的错误:

3128
144.217.31.225
Traceback (most recent call last):
File "./script.py", line 32, in <module>
search=browser.find_element_by_xpath("//input[@title='Search']")
File "/home/matt/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 313, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/home/matt/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 791, in find_element
'value': value})['value']
File "/home/matt/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
self.error_handler.check_response(response)
File "/home/matt/.local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //input[@title='Search']

它成功加载页面,但无法在搜索栏中输入文本。我手动接管并在搜索栏中输入“我的 IP”,但 IP 没有改变。

最佳答案

下面的代码有效,或者尽管谷歌不喜欢我使用带有selenium的代理这一事实,这就是我将地址更改为whatsmyipaddress.com的原因。

from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from time import sleep


HOST = "144.217.31.225"
PORT = "3128"
def my_proxy(PROXY_HOST,PROXY_PORT):
fp = webdriver.FirefoxProfile()
# Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5
print PROXY_PORT
print PROXY_HOST
fp.set_preference("network.proxy.type", 1)
fp.set_preference("network.proxy.http",PROXY_HOST)
fp.set_preference("network.proxy.http_port",int(PROXY_PORT))
fp.set_preference("network.proxy.ssl",PROXY_HOST)
fp.set_preference("network.proxy.ssl_port",int(PROXY_PORT))
fp.set_preference("general.useragent.override","whater_useragent")
fp.update_preferences()
return webdriver.Firefox(firefox_profile=fp)



browser = my_proxy(HOST,PORT)
browser.get('http://whatismyipaddress.com/')

sleep(5)

search=browser.find_element_by_xpath("//input[@title='Search']")
search.send_keys('my ip')

感谢@SAZ 提供的解决方案。

关于python - 配置 Selenium Firefox 代理设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45230156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com