我一直在尝试使用selenium和代理服务器大约2天,但仍然无法做到。我从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没有改变。
答案 0 :(得分:2)
以下代码有效,或者谷歌不喜欢我使用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的解决方案。