我想使用Selenium单击菜单下拉列表中的元素。不幸的是,我要单击的元素一直位于列表的底部。我的Selenium Python Chrome代码在前台模式下可以完美运行,但是当我在后台/无头模式下运行时,出现超时或不可点击的错误(请参见下面的注释)。这是使用实际公共URL的代码,因此您可以根据需要对其进行测试:
# define chrome options
options = webdriver.ChromeOptions()
# make sure browser is maximized so all elements are visible
options.add_argument('--start-maximized')
# define driver
driver = webdriver.Chrome(chrome_options=options)
# define function used to get button
def getbutton(driver, xpath, waittime=10):
return WebDriverWait(driver, waittime).until(EC.element_to_be_clickable((By.XPATH, xpath)))
# navigate to url
driver.get('http://oasis.caiso.com/mrioasis/logon.do')
# another two different ways to make sure browser window is maximized
# I'm going through this trouble just in case selenium needs the window
# maximized to see the element
driver.fullscreen_window()
driver.maximize_window()
# choose dropdown menu
button1 = getbutton(driver, "//span[contains(@id, 'LBL_')][text()='ATLAS REFERENCE']", waittime=120)
hover = ActionChains(driver).move_to_element(button1)
hover.perform()
time.sleep(5)
# see about navigating to the second to last button in the drop down
# menu list - not what I want, but a test
testing = True
if testing:
# move to second to last button - interestingly this works in foregraound AND background / headless modes
button1a = getbutton(driver, "//span[contains(@id, 'LBL_')][text()='Intertie Scheduling Limit and Tie Mapping']")
hover = ActionChains(driver).move_to_element(button1a)
# THIS WORKS in foreground AND backgroubd modes
hover.perform()
time.sleep(10)
# Now try and click the button I really want - the LAST button in the drop down menu list
button2 = getbutton(driver, "//span[contains(@id, 'LBL_')][text()='Master Control Area Generating Capability List']", waittime=120)
button2.click()
button2 = getbutton失败,并显示以下错误消息:
selenium.common.exceptions.TimeoutException
有趣的是,如果我将测试设置为False,则会收到以下错误消息:
selenium.common.exceptions.WebDriverException: Message: unknown error:
Element <span ...></table>">Master Control Area Generating Capability List</span>
is not clickable at point (128, 599). Other element would receive the click
硒似乎无法在后台/无头模式下看到元素,即使在前台模式下运行时这些异常都不会发生。
我正在Windows Server 2012 R2中运行Python,以了解它的价值。
感谢您的帮助!
答案 0 :(得分:1)
我在您的代码中做了几处更改,并且不再出现错误。我不确定它是否正在加载正确的网页,因为它没有头,而且一切似乎都在动态加载。
# another two different ways to make sure browser window is maximized
# I'm going through this trouble just in case selenium needs the window
# maximized to see the element
#driver.fullscreen_window()
#driver.maximize_window()
# choose dropdown menu
button1 = driver.find_element_by_xpath("//span[contains(@id, 'LBL_')][text()='ATLAS REFERENCE']")
hover = ActionChains(driver)
hover.move_to_element(button1)
# see about navigating to the second to last button in the drop down
# menu list - not what I want, but a test
testing = True
if testing:
# move to second to last button - interestingly this works in foregraound AND background / headless modes
button1a = driver.find_element_by_xpath("//span[contains(@id, 'LBL_')][text()='Intertie Scheduling Limit and Tie Mapping']")
hover.move_to_element(button1a)
# THIS WORKS in foreground AND backgroubd modes
# Now try and click the button I really want - the LAST button in the drop down menu list
button2 = driver.find_element_by_xpath("//span[contains(@id, 'LBL_')][text()='Master Control Area Generating Capability List']")
hover.click(button2)
hover.perform()
我使用.find_element_by_xpath()
选择元素,并排队所有ActionChains,最后使用hover.perform()
执行它们。
经过进一步分析,我无法使用driver.find_element_by_id("masterControlAreaGenCapGrid_GRID_LABEL").text
找到“ masterControlAreaGenCapGrid_GRID_LABEL”元素
当浏览器不是没有头的时候它就可以工作...
答案 1 :(得分:0)
所以我终于弄清楚了问题所在:运行带有“无论用户是否登录”选项的Windows任务计划程序,只会打开一个无法调整大小的小型浏览器(1024x768),即使提供了所有不错的建议这里。所以我根本看不到我想要单击的元素。
看到相同的问题已在此处解决:screen resolution in mode "Run whether user is logged on or not", in windows task scheduler
因此,不太理想的解决方法是仅在用户登录时运行。
感谢您的所有帮助!