我试图单击将下载Excel报告的元素。我使用以下代码正确定位了元素。但是,我无法通过使用.click或Select()来单击它。
geom_point
-。click返回“ selenium.common.exceptions.ElementNotInteractableException:消息:元素不可交互”
-Select()返回“ selenium.common.exceptions.UnexpectedTagNameException:消息:选择仅适用于元素,不适用于“
如何单击元素?当我检查该元素时,它看起来像这样:
driver.find_element_by_xpath(element)
答案 0 :(得分:0)
您可以尝试使用Javascript单击来解决ElementNotInteractable
异常:
element_to_click = driver.find_element_by_xpath("//a[text()='Export to Excel']")
driver.execute_script("arguments[0].click();", element_to_click)
通过调用WebDriverWait
来确保单击之前确保元素已完全加载的另一种方法:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element_to_click = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//a[text()='Export to Excel']")))
driver.execute_script("arguments[0].click();", element_to_click)
答案 1 :(得分:0)
使用动作类的另一种解决方案,将有助于解决ElementNotInteractable
异常。
解决方案1:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.common.action_chains import ActionChains
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//a[starts-with(.,Export to Excel')]")))
actionChains.move_to_element(element).click().perform()
解决方案2:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.common.action_chains import ActionChains
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//a[@class='mrx']")))
actionChains.move_to_element(element).click().perform()