我正试图用selenium和python在网页上注销,但目前没有成功。为了注销,我需要单击网页右上角的链接,它将打开一个小的下拉窗口,然后我可以单击此下拉窗口中的“注销”图标。这是这个下拉窗口的图片。
以及下拉窗口中此注销图标的检查代码
现在在我的python代码中,我可以打开下拉窗口,但是如果我要单击注销图标,我会不断得到“selenium.common.exceptions.ElementNotVisibleException”的异常
这是我的密码:
try:
# to click the link so that the drop-down window opens
action = ActionChains(self.driver)
dropdownwindow= self.driver.find_element_by_xpath("//span[@class='ssobanner_logged']/img")
action.move_to_element(dropdownwindow).perform()
dropdownwindow.click()
# try to click the logout icon in the drop-down so that user may logout
logoutLink = self.driver.find_element_by_xpath(
"//*[@id='ctl00_HeaderAfterLogin1_DL_Portals1']/tbody/tr/td[4]/a/img")
action.move_to_element(logoutLink).perform()
logoutLink.click()
return True
except Exception as e:
self.logger.info(e)
raise
return False
我在运行时遇到了这样的异常
selenium.common.exceptions.NoSuchElementException:
Message: no such element: Unable to locate element:
{"method":"xpath","selector":"//*[@id='ctl00_HeaderAfterLogin1_DL_Portals1']/tbody/tr/td[4]/a/img"}
除了我使用的xpath之外,还有谁知道更好的处理方法吗
一旦下拉窗口打开并单击文本为“注销”的图标,您需要为
element_to_be_clickable()
导入WebDriverWait,您可以使用以下任一Locator Strategies:使用
CSS_SELECTOR
:使用
XPATH
:注意:您必须添加以下导入:
问题很可能是单击下拉菜单后,下拉菜单未完全展开/呈现。虽然
time.sleep(1)
命令可能是一个潜在的修复程序,但更合适的修复程序是使用WebDriverWait
的动态等待:expected_conditions
也可以使用visibility_of_element_located
或presence_of_element_located
等待相关问题 更多 >
编程相关推荐