Python / Selenium,如果 'This site can’t be reached' 则执行其他操作
Python / Selenium, if 'This site can’t be reached' then do something else
我有一个测试一些代理的脚本,如果代理不工作,将显示页面 “无法访问此站点”,如果发生这种情况,我希望代码能够继续并使用不同的代理重试,但 selenium 仍然卡在“无法访问此站点”页面上。
我试了一个try/exept
子句,但是selenium仍然停留在错误页面上,我也试了except TimeoutError
,但还是卡住了。
代码:
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
import time
proxy_ip_port = 'ip:port'
proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = proxy_ip_port
proxy.ssl_proxy = proxy_ip_port
capabilities = webdriver.DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)
# replace 'your_absolute_path' with your chrome binary absolute path
driver = webdriver.Chrome('C:/chromedriver.exe', desired_capabilities=capabilities)
try:
driver.get('https://www.tiktok.com/signup/phone-or-email/email')
except TimeoutError:
print("Proxy: ", proxy_ip_port, "invalid")
输出:
C:\Users\fkahd\PycharmProjects\tkinter\venv\Scripts\python.exe C:/Users/fkahd/PycharmProjects/tkinter/proxy2.py
C:\Users\fkahd\PycharmProjects\tkinter\proxy2.py:17: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome('C:/chromedriver.exe', desired_capabilities=capabilities)
Process finished with exit code 0
“无法访问此站点”页面:
感谢您的帮助!
您可以指定页面加载超时值,并捕获 TimeoutException 类型。
我能够通过通用异常处理捕获失败(为了完整性我输入了一个环回代理...):
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.common.exceptions import TimeoutException
proxy_ip_port = '10.27.0.0:80'
proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = proxy_ip_port
proxy.ssl_proxy = proxy_ip_port
capabilities = webdriver.DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)
# replace 'your_absolute_path' with your chrome binary absolute path
driver = webdriver.Chrome()
driver.set_page_load_timeout(5)
try:
driver.get('https://www.tiktok.com/signup/phone-or-email/email')
except TimeoutException:
print("Proxy: ", proxy_ip_port, "invalid")
我有一个测试一些代理的脚本,如果代理不工作,将显示页面 “无法访问此站点”,如果发生这种情况,我希望代码能够继续并使用不同的代理重试,但 selenium 仍然卡在“无法访问此站点”页面上。
我试了一个try/exept
子句,但是selenium仍然停留在错误页面上,我也试了except TimeoutError
,但还是卡住了。
代码:
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
import time
proxy_ip_port = 'ip:port'
proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = proxy_ip_port
proxy.ssl_proxy = proxy_ip_port
capabilities = webdriver.DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)
# replace 'your_absolute_path' with your chrome binary absolute path
driver = webdriver.Chrome('C:/chromedriver.exe', desired_capabilities=capabilities)
try:
driver.get('https://www.tiktok.com/signup/phone-or-email/email')
except TimeoutError:
print("Proxy: ", proxy_ip_port, "invalid")
输出:
C:\Users\fkahd\PycharmProjects\tkinter\venv\Scripts\python.exe C:/Users/fkahd/PycharmProjects/tkinter/proxy2.py
C:\Users\fkahd\PycharmProjects\tkinter\proxy2.py:17: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome('C:/chromedriver.exe', desired_capabilities=capabilities)
Process finished with exit code 0
“无法访问此站点”页面:
感谢您的帮助!
您可以指定页面加载超时值,并捕获 TimeoutException 类型。
我能够通过通用异常处理捕获失败(为了完整性我输入了一个环回代理...):
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.common.exceptions import TimeoutException
proxy_ip_port = '10.27.0.0:80'
proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = proxy_ip_port
proxy.ssl_proxy = proxy_ip_port
capabilities = webdriver.DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)
# replace 'your_absolute_path' with your chrome binary absolute path
driver = webdriver.Chrome()
driver.set_page_load_timeout(5)
try:
driver.get('https://www.tiktok.com/signup/phone-or-email/email')
except TimeoutException:
print("Proxy: ", proxy_ip_port, "invalid")