如何让 Selenium 在层次结构下拉列表中循环
Posted
技术标签:
【中文标题】如何让 Selenium 在层次结构下拉列表中循环【英文标题】:How to get Selenium to cycle through a hierarchy dropdown 【发布时间】:2022-01-15 04:01:56 【问题描述】:我有这个链接,我必须从:https://partsurfer.hp.com 抓取部分内容。 要到达每个部分,我首先需要循环单击一系列以使元素可见。下图就是一个例子:
我的代码在经过一个周期后卡住了。我似乎无法让它经历几次。请看一下我的代码。如果有任何想法可以帮助我正确编写循环,我将不胜感激。
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from time import sleep
options1 = webdriver.ChromeOptions()
options1.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(executable_path="~~chromedriver.exe", options=options1)
url = 'https://partsurfer.hp.com'
driver.get(url)
sleep(5)
# deal with popup
driver.find_element_by_xpath("//button[@id='onetrust-accept-btn-handler']").click()
# select country and reload the page
dropdowns = Select(driver.find_element_by_id('ctl00_BodyContentPlaceHolder_ddlCountry'))
dropdowns.select_by_value('ZA')
driver.refresh()
sleep(5)
# click hierarchy and cycle through the list to the parts
hierarchy_click = driver.find_element_by_xpath("//a[@id='ctl00_BodyContentPlaceHolder_aHierarchy']/span[@class='ie_bg']/span").click()
sleep(5)
category_list = driver.find_elements_by_xpath("//table[@width='650']/tbody/tr/td/a")
for a in category_list:
breadcrumb1 = a.text
print(breadcrumb1)
a.click()
sleep(2)
series_list = driver.find_elements_by_xpath("//div[@id='ctl00_BodyContentPlaceHolder_HierarchyTreen1Nodes']/table/tbody/tr//a")
for b in series_list:
breadcrumb2 = b.text
print(breadcrumb2)
b.click()
sleep(2)
series_2 = driver.find_elements_by_xpath("//div[@id='ctl00_BodyContentPlaceHolder_HierarchyTreen2Nodes']/table/tbody/tr//a")
for c in series_2:
breadcrumb3 = c.text
print(breadcrumb3)
c.click()
sleep(2)
series_3 = driver.find_elements_by_xpath("//div[@id='ctl00_BodyContentPlaceHolder_HierarchyTreen3Nodes']/table/tbody/tr//a")
for d in series_3:
breadcrumb4 = d.text
print(breadcrumb4)
d.click()
sleep(2)
series_4 = driver.find_elements_by_xpath("//div[@id='ctl00_BodyContentPlaceHolder_HierarchyTreen4Nodes']/table/tbody/tr//a")
for e in series_4:
breadcrumb5 = e.text
print(breadcrumb5)
e.click()
sleep(2)
models = driver.find_elements_by_xpath("//table[@class='table_sortable']/tbody//a")
for model in models:
model_num = model.text
print(model_num)
model.click()
sleep(5)
# model number = //span[@id='ctl00_BodyContentPlaceHolder_lblProductNumber']
table_rows = driver.find_elements_by_xpath("//div[@id='ctl00_BodyContentPlaceHolder_dvProdinfo']/table/tbody/tr")
for row in table_rows:
print(row.text)
我的代码不完整,因此希望获得任何输入以提高其效率。
【问题讨论】:
欢迎使用 ***,如果您需要调试器,请转到 Debugoverflow 使用索引来获取下一个元素所以获取总元素然后循环。 【参考方案1】:wait=WebDriverWait(driver,10)
driver.get('https://partsurfer.hp.com')
# deal with popup
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button#onetrust-accept-btn-handler"))).click()
# select country and reload the page
dropdowns = Select(driver.find_element_by_id('ctl00_BodyContentPlaceHolder_ddlCountry'))
dropdowns.select_by_value('ZA')
# click hierarchy and cycle through the list to the parts
wait.until(EC.element_to_be_clickable((By.XPATH,"//a[@id='ctl00_BodyContentPlaceHolder_aHierarchy']/span[@class='ie_bg']/span"))).click()
category_list = driver.find_elements_by_xpath("//table[@width='650']/tbody/tr/td/a")
for i in range(1,len(category_list)):
wait.until(EC.element_to_be_clickable((By.XPATH,"(//table[@width='650']/tbody/tr/td/a)["+str(i)+"]"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#ctl00_BodyContentPlaceHolder_HierarchyTreet0"))).click()
这是一个小演示,可以通过顶层。您想使用***层次结构来重置自己。
进口:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
【讨论】:
想知道你为什么要driver.refresh()
?
哦忘了删除
@undetected Selenium driver.refresh()
是必要的,因为当您更改位置时页面需要重新加载。它不会自动这样做以上是关于如何让 Selenium 在层次结构下拉列表中循环的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Selenium 和 Java 从非选择下拉列表中单击并选择一个选项
RobotFramework Selenium:如何从多选下拉列表中选择多个选项?
角度 6 中带有第 n 个子项的层次结构下拉列表。我想选择父复选框需要隐藏所有子复选框
如何使用来自具有特殊设置的网站的Selenium从下拉列表中选择值 - Python