可点击元素的问题 - Selenium 4,Python

问题描述 投票:0回答:1

我尝试找到并单击网站上的链接。 但由于某种原因,selenium 找不到链接并超时。 如果没有显式等待,则找不到元素。

我的代码

url = "https://lineascan.build/address/0x578705c60609c9f02d8b7c1d83825e2f031e35aa#writeContract"
driver.get(url)

    # Wait for the "Connect to Web3" button to be clickable
wait = WebDriverWait(driver, 10)
connect_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Connect to Web3')]")))
connect_button.click()

知道为什么吗?

python-3.x selenium-webdriver
1个回答
0
投票

您的代码存在一些问题。

  1. “连接到 Web3”链接不是按钮,而是 A 标签。
  2. “连接到 Web3”链接位于 IFRAME 中,因此您需要先将上下文切换到包含的 IFRAME,然后再尝试查找该链接。

修复链接定位器并向 IFRAME 添加开关,我想出了下面的工作代码...

url = 'https://lineascan.build/address/0x578705c60609c9f02d8b7c1d83825e2f031e35aa#writeContract'
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(url)

wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "writecontractiframe")))
wait.until(EC.element_to_be_clickable((By.ID, "connectStatus"))).click()
# when you are done working with elements inside this IFRAME, use the line below to switch back to the default context
driver.switch_to.default_content
© www.soinside.com 2019 - 2024. All rights reserved.