如何修复“元素<option>无法滚动到视图”

时间:2019-08-26 19:44:53

标签: python selenium selenium-webdriver

我尝试使用Selenium Webdriver从下拉列表中选择一个值,但是我只会得到错误。

我只想从OS Dropdown中选择Ubuntu 16.04。 我尝试单击下拉列表,然后单击Ubuntu 16.04元素(源代码中有3个)。我尝试了wait.until方法,但没有成功。

from selenium.webdriver.support.select import Select
from selenium import webdriver

url = 'https://clients.hostwinds.com/buycloud/289'
driver = webdriver.Firefox()
driver.get(url)
firstname = driver.find_element_by_name("firstname").send_keys('First')
lastname = driver.find_element_by_name("lastname").send_keys('Last')
email = driver.find_element_by_name("email").send_keys('mail@example.com')
password = driver.find_element_by_name("password").send_keys('123456789')
submit = driver.find_element_by_css_selector("#signup > div:nth-child(1) > div:nth-child(2) > form:nth-child(1) > div:nth-child(4) > div:nth-child(1) > div:nth-child(1) > input:nth-child(1)").click()
## next page
state = Select(driver.find_element_by_name("state")).select_by_index(2) ## works fine
## Here the problem begins: I need to choose Ubuntu 16.04 but all of my attemts failed

os = driver.find_element_by_xpath('//*[@id="inputConfigOption337"]')
os.location_once_scrolled_into_view
Select(os).select_by_index(5)

我收到以下错误:

  

selenium.common.exceptions.ElementNotInteractableException:消息:元素无法滚动到视图中

1 个答案:

答案 0 :(得分:0)

请参考链接的文章,该文章解决了引发的滚动错误: Scrolling to element using webdriver?

我在Firefox和Python中使用Selenium时遇到了此问题。我最终在Firefox上使用了scrollIntoView方法,因为在测试它们时其他选项的行为不一致。 (https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView)。