无法使用硒单击网页上可用的按钮
Failed to click on a button available on a webpage using selenium
我一直在想办法在这个 Download base consolidada
上点击可见的按钮=15=]。该按钮似乎在 iframe 中。看来我已经能够切换到 iframe
并使用我在以下脚本中定义的一些 xpath 找到该按钮的元素。但是,在单击该按钮时脚本会抛出一些错误。
我正在尝试:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
link = "https://www.anbima.com.br/pt_br/autorregular/matriz-de-probabilidade-de-resgates.htm"
with webdriver.Chrome() as driver:
wait = WebDriverWait(driver,20)
driver.get(link)
wait.until(EC.frame_to_be_available_and_switch_to_it(wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "iframe")))))
wait.until(EC.element_to_be_clickable((By.XPATH,"//span[@class='textRun'][.='Download base consolidada']"))).click()
这是我得到的错误:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <span class="textRun" style="font-size: 14pt; font-weight: bold; color: rgb(0, 135, 202);">...</span> is not clickable at point (345, 90). Other element would receive the click: <rect x="4.02685069008783" y="1.961104140526976" width="244.63136762860728" height="42.18820577164366" rx="8" ry="8" style="vector-effect: non-scaling-stroke; stroke-width: 3px; stroke: rgb(0, 149, 217); stroke-opacity: 1; fill: rgb(243, 242, 241); fill-opacity: 0;"></rect>
(Session info: chrome=98.0.4758.102)
Stacktrace:
Backtrace:
Ordinal0 [0x00307AC3+2587331]
Ordinal0 [0x0029ADD1+2141649]
Ordinal0 [0x00193BB8+1063864]
Ordinal0 [0x001C65FF+1271295]
and so on--------------------
How can I click on the aforementioned button using selenium?
driver.get('https://www.anbima.com.br/pt_br/autorregular/matriz-de-probabilidade-de-resgates.htm')
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "iframe")))))
WebDriverWait(driver, 40).until(EC.visibility_of_element_located((By.XPATH, "(//*[@data-automation-type='visualContainerHost']//div[contains(@aria-label, 'Web URL')])[1]"))).click()
time.sleep(5)
driver.quit()
这给了我输出:
Process finished with exit code 0
这意味着没有错误并且代码 运行 成功。
下面是通过代码下载的文件截图:
Excel file snapshot
这是因为您尝试点击的元素不是接受实际点击的元素,而是 rect 元素。
尝试用包含 rect 元素的 div 替换 span 的 Xpath,它应该可以工作。
wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@aria-describedby='visualsEnterHint-e780d1b86be1529ccdf0']/div[3]/div/visual-modern/div/div"))).click()
我一直在想办法在这个 Download base consolidada
上点击可见的按钮=15=]。该按钮似乎在 iframe 中。看来我已经能够切换到 iframe
并使用我在以下脚本中定义的一些 xpath 找到该按钮的元素。但是,在单击该按钮时脚本会抛出一些错误。
我正在尝试:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
link = "https://www.anbima.com.br/pt_br/autorregular/matriz-de-probabilidade-de-resgates.htm"
with webdriver.Chrome() as driver:
wait = WebDriverWait(driver,20)
driver.get(link)
wait.until(EC.frame_to_be_available_and_switch_to_it(wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "iframe")))))
wait.until(EC.element_to_be_clickable((By.XPATH,"//span[@class='textRun'][.='Download base consolidada']"))).click()
这是我得到的错误:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <span class="textRun" style="font-size: 14pt; font-weight: bold; color: rgb(0, 135, 202);">...</span> is not clickable at point (345, 90). Other element would receive the click: <rect x="4.02685069008783" y="1.961104140526976" width="244.63136762860728" height="42.18820577164366" rx="8" ry="8" style="vector-effect: non-scaling-stroke; stroke-width: 3px; stroke: rgb(0, 149, 217); stroke-opacity: 1; fill: rgb(243, 242, 241); fill-opacity: 0;"></rect>
(Session info: chrome=98.0.4758.102)
Stacktrace:
Backtrace:
Ordinal0 [0x00307AC3+2587331]
Ordinal0 [0x0029ADD1+2141649]
Ordinal0 [0x00193BB8+1063864]
Ordinal0 [0x001C65FF+1271295]
and so on--------------------
How can I click on the aforementioned button using selenium?
driver.get('https://www.anbima.com.br/pt_br/autorregular/matriz-de-probabilidade-de-resgates.htm')
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "iframe")))))
WebDriverWait(driver, 40).until(EC.visibility_of_element_located((By.XPATH, "(//*[@data-automation-type='visualContainerHost']//div[contains(@aria-label, 'Web URL')])[1]"))).click()
time.sleep(5)
driver.quit()
这给了我输出:
Process finished with exit code 0
这意味着没有错误并且代码 运行 成功。
下面是通过代码下载的文件截图: Excel file snapshot
这是因为您尝试点击的元素不是接受实际点击的元素,而是 rect 元素。 尝试用包含 rect 元素的 div 替换 span 的 Xpath,它应该可以工作。
wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@aria-describedby='visualsEnterHint-e780d1b86be1529ccdf0']/div[3]/div/visual-modern/div/div"))).click()