我尝试找到并单击网站上的链接。 但由于某种原因,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()
知道为什么吗?
您的代码存在一些问题。
修复链接定位器并向 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