如何解决在Python中使用Selenium显式等待
我有一个IP列表,这些IP用于当前已部署的约600个调制解调器。这些调制解调器中的某些将可访问,而某些则可能不是由于连接性或电池已耗尽。基本上想遍历所有IP,并明确等待包含调制解调器固件版本的底部文本字符串。如果工作站未加载,我希望它添加“无连接”来代替IP。我想将该固件附加到一个空列表中,然后将该列表打印到csv中。
我的主要障碍似乎是WebDriverWait(driver,30)。直到我以前从同一定位器提取代码时,直到代码的一部分似乎总是引发异常。我看到浏览器负载,然后它仍然会引发异常。我已经尝试了几种“标准预期条件”,但似乎都没有用。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
import os
import csv
prefix = 'http://'
suffix = ':9191'
fwlist = []
iplist = ["###.###.###.###",...]
class presence_of_element_located(object):
""" An expectation for checking that an element is present on the DOM
of a page. This does not necessarily mean that the element is visible.
locator - used to find the element
returns the WebElement once it is located
"""
def __init__(self,locator):
self.locator = locator
def __call__(self,driver):
return _find_element(driver,self.locator)
for ip in iplist:
url = prefix + ip + suffix
# instantiate a chrome options object so you can set the size and headless preference
# some of these chrome options might be uncessary but I just used a boilerplate
# change the <path_to_download_default_directory> to whatever your default download folder is located
chrome_options = Options()
#chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920x1080")
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--verbose')
chrome_options.add_experimental_option("prefs",{
"download.default_directory": r"Z:\Python\Scaping\Chromedriver\chromedriver.exe","download.prompt_for_download": False,"download.directory_upgrade": True,"safebrowsing_for_trusted_sources_enabled": False,"safebrowsing.enabled": False
})
#chrome_options.add_argument('--disable-software-rasterizer')
# initialize driver object and change the <path_to_chrome_driver> depending on your directory where your chromedriver should be
driver = webdriver.Chrome(chrome_options=chrome_options,executable_path=r"Z:\Python\Scaping\Chromedriver\chromedriver.exe")
# get request to target the site selenium is active on
driver.get(f'{url}')
try:
WebDriverWait(driver,30,.5).until(presence_of_element_located((By.ID,"login_screen")))
print(f'{url} is ready')
except:
print(f'Timeout for {url}')
fw = 'No connection'
fwlist.append(fw)
text = driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/div[4]").text
fw = text.split(' ',3)
fwlist.append(fw[3])
print(fwlist)
driver.close()
with open('Scaping\PGE-FW.csv','w',newline='') as f:
writer = csv.writer(f)
writer.writerows(fw)
driver.quit()
为了进行测试,我使用了一个工作IP和一个不工作IP,但是在30秒内我看到一个工作IP可以正常工作,但这总是一个例外。
下面是html的图像,其中包含等待的ID和所需数据的xpath。我真的不知道为大家提供调制解调器内部网页html的方法,但是如果有人可以,请告诉我。因此,我想我有两个问题:我是否犯了一个明显的错误,为什么这不起作用,或者也许有更好的方法来解决这个问题?
解决方法
忘记了导入。
Needed:
from selenium.webdriver.support import expected_conditions as EC
From:
WebDriverWait(driver,30,.5).until(presence_of_element_located((By.ID,"login_screen")))
To:
WebDriverWait(driver,.5).until(EC.presence_of_element_located((By.ID,"login_screen")))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。