Python 3.x 蟒蛇硒;如何单击锚元素?
我正在尝试单击一个元素,该元素将下载Excel报告。我使用下面的代码正确地定位了元素。但是,我无法使用单击它。单击或选择() -。单击返回“selenium.common.exceptions.ElementNotInteractiableException:消息:元素不可交互” -Select()返回“selenium.common.exceptions.UnexpectedTagNameException:消息:Select仅适用于元素,不适用于” 如何单击该元素?当我检查元素时,它看起来像这样:Python 3.x 蟒蛇硒;如何单击锚元素?,python-3.x,selenium,Python 3.x,Selenium,我正在尝试单击一个元素,该元素将下载Excel报告。我使用下面的代码正确地定位了元素。但是,我无法使用单击它。单击或选择() -。单击返回“selenium.common.exceptions.ElementNotInteractiableException:消息:元素不可交互” -Select()返回“selenium.common.exceptions.UnexpectedTagNameException:消息:Select仅适用于元素,不适用于” 如何单击该元素?当我检查元素时,它看起来像
<a href="#" onclick="tablesToExcel(['summary','table1'], ['Report Profit'], 'myfile.xls')">
<img src="/portalDispensary/vendors/tableExport/icon/xls.png" width="24px" class="mrx">
"Export to Excel"
</a>
您可以尝试使用Javascript单击来解决
elementnotinteractiable
异常:
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)
另一个使用action类的解决方案,它将有助于解决
ElementNotInteractiable
异常
解决方案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()
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()
解决方案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[starts-with(.,Export to Excel')]")))
actionChains.move_to_element(element).click().perform()
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()
解决了,通过Javascript点击执行它