python硒,无法单击元素

时间:2020-05-21 19:12:01

标签: python selenium web-scraping

我的硒有问题。我加载了此页面:Investing 3M,并且我想单击将其显示在弹出窗口中。

investing got it window

我已经尝试过使用下面的代码,但是它不起作用:

driver = webdriver.Chrome()
driver.get("https://www.investing.com/equities/3m-co-financial-summary")
driver.implicitly_wait(10)
x =driver.find_elements_by_xpath("/html/body/div[8]/div[1]/div/div[2]/div[2]/a[1]")
print(len(x))
print(x)

有人可以提供任何解决方案吗?

1 个答案:

答案 0 :(得分:0)

该元素位于 iframe 内部。您需要先切换到框架。

要处理动态元素,您需要

引出WebDriverWait()并等待frame_to_be_available_and_switch_to_it()并跟随xpath

引出WebDriverWait()并等待element_to_be_clickable()并跟随xpath

代码

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 import webdriver

driver = webdriver.Chrome()
driver.get("https://www.investing.com/equities/3m-co-financial-summary")
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@title='TrustArc Cookie Consent Manager']")))
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//a[text()='Got it']"))).click()

点击后的浏览器快照

enter image description here

相关问题