【发布时间】:2019-02-27 19:17:54
【问题描述】:
我正在尝试循环浏览此网址上的下拉菜单:https://www.accuform.com/safety-sign/danger-danger-authorized-personnel-only-MADM006
因此,例如,第一个下拉菜单 - 在选项下 - 列出了不同的材料,我想依次选择每个材料,然后从网页中收集一些其他信息,然后再转到下一个材料。这是我当前的代码:
driver = webdriver.Firefox()
driver.get('https://www.accuform.com/safety-sign/danger-danger-authorized-personnel-only-MADM006')
time.sleep(3)
driver.find_element_by_id('x-mark-icon').click()
select = Select(driver.find_element_by_name('Wiqj7mb4rsAq9LB'))
options = select.options
optionsList = []
driver.find_elements_by_class_name('select-wrapper')[0].click()
element = driver.find_element_by_xpath("//select[@name='Wiqj7mb4rsAq9LB']")
actions = ActionChains(driver)
actions.move_to_element(element).perform()
# driver.execute_script("arguments[0].scrollIntoView();", element)
for option in options: #iterate over the options, place attribute value in list
optionsList.append(option.get_attribute("value"))
for optionValue in optionsList:
print("starting loop on option %s" % optionValue)
# select = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@name='Wiqj7mb4rsAq9LB']")))
# select = Select(select)
select.select_by_value(optionValue)
我从循环开始,但得到了这个错误:
ElementNotInteractableException: Message: Element <option> could not be scrolled into view
然后我添加了 webdriverwait 并得到一个 TimeoutException 错误。
然后我意识到我可能应该单击保存下拉列表的包装器,所以我添加了单击,它确实弹出了菜单,但我仍然得到了 TimeoutException。
所以我想,也许我应该移动到元素,我用动作链线尝试过,我得到了这个错误
WebDriverException: Message: TypeError: rect is undefined
我试图通过使用此代码来避免该错误:
# driver.execute_script("arguments[0].scrollIntoView();", element)
这又导致了超时异常。
我对 Python 和 Selenium 很陌生,基本上只是在修改类似问题的 SO 答案中的代码,但没有任何效果。
我正在使用 python 3.6 和当前版本的 Selenium 和 firefox webdriver。
如果有任何不清楚的地方或者您需要更多信息,请告诉我。
非常感谢!
编辑:根据 Kajal Kunda 的回答和 cmets,我已将代码更新为以下内容:
`material_dropdown = driver.find_element_by_xpath("//input[@class='select-
dropdown']")
driver.execute_script("arguments[0].click();", material_dropdown)
materials=driver.find_elements_by_css_selector("div.select-wrapper
ul.dropdown-content li")
for material in materials:
# material_dropdown =
driver.find_element_by_xpath("//input[@class='select-dropdown']")
# driver.execute_script("arguments[0].click();", material_dropdown)
# materials=driver.find_elements_by_css_selector("div.select-wrapper ul.dropdown-content li")
material_ele=material.find_element_by_tag_name('span')
if material_ele.text!='':
material_ele.click()
time.sleep(5)
price = driver.find_element_by_class_name("dataPriceDisplay")
print(price.text)`
结果是它成功打印了第一种材料的价格,但随后又返回:
StaleElementReferenceException: Message: The element reference of <li class=""> is stale;...
我尝试了在循环内外添加散列线的变体,但总是得到 StaleElementReferenceException 错误的版本。
有什么建议吗?
谢谢!
【问题讨论】:
-
你能解决这个问题吗?
标签: python selenium web-scraping