python - 在 Python 中使用 Selenium 的元素不可交互错误
问题描述
我正在尝试从网站上抓取一些数据,并且每隔一段时间就会出现一个弹出窗口。我正在尝试构建一个功能,该功能将通过按“x”图标来删除弹出窗口。我尝试了多种单击它的方法,但每次我似乎都得到相同的“元素不可交互”错误。
以下是我的 3 种不同尝试:
try:
if driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/a').is_displayed():
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div/div[1]/a")))
driver.execute_script("arguments[0].click();", element)
except Exception as e:
print(e)
try:
if driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/a').is_displayed():
element = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/a')
ActionChains(driver).move_to_element(element).click().perform()
except Exception as e:
print(e)
try:
if driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/a').is_displayed():
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//a[text()='x']")))
driver.execute_script("arguments[0].click();", element)
except Exception as e:
print(e)
html 代码并没有说“x”是一个按钮,而是说它是位于标签内的文本。我对html不是很熟悉,所以如果你需要它来帮助我可以提供。
非常感谢
编辑:这是我试图按下的按钮的 html 代码:
<a data-trigger="dismiss.close" style="background:rgb(0,0,0);border-radius:100%;box-shadow:rgba(0,0,0,.6) 0 2px 6px;color:rgb(255,255,255);font-family:sans-serif;font-size:20px;font-weight:400;display:block;height:32px;line-height:32px;position:absolute;right:-15px;text-align:center;top:-15px;width:32px;cursor:pointer;bottom:auto;left:auto;z-index:99;min-width:0;max-width:none;min-height:0;max-height:none;">×</a>
解决方案
推荐阅读
- python - Python 多处理仅在第一次运行后快速
- javascript - 缩放 y 轴以适合多折线图显示中的数据
- c# - HttpResponseMessage 序列化为 xml
- python - 通过 websockets 抓取investing.com
- git - 如何使用 git 更新已更新的开源应用程序的本地版本?
- python - TensorFlow 对象检测运行良好但边界框关闭,如何获得正确的坐标?
- java - JMS 消息侦听器调用程序失败,原因:标识符包含无效的 JMS 标识符字符“-”:“x-request-id”
- javascript - Sequelize hasMany 和 belongsTo 问题
- c# - C# SQL 插入说“重复变量”
- r - R中具有长格式纵向数据的交叉表/频率表