I'm very new to Selenium and am trying to build a scraper on this site that clicks on the "16. token URI" button, enters a number into the input field, and clicks the the Query button. But no matter how I define the element to be clicked, Selenium clicks on a different button, even thought I've copied the XPATH exactly. Obviously I can't move onto the next two steps until I solve this one. How do I force Selenium to scroll down to the 16th button and click it?
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
import time
service = Service(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
driver.maximize_window()
driver.get("https://etherscan.io/address/0xd0318da435dbce0b347cc6faa330b5a9889e3585#readContract")
assert "ASMBrain" in driver.title
try:
delay = 5
WebDriverWait(driver, delay).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "readcontractiframe")))
element = driver.find_element(By.XPATH, "//*[@id='readHeading16']/a")
# ActionChains(driver).move_to_element(element).click().perform()
driver.execute_script("arguments[0].scrollIntoView(true);", element)
WebDriverWait(driver, delay).until(EC.visibility_of((element))).click()
# driver.find_element(By.XPATH, "//input[@id='input_16_1']").send_keys('10')
# driver.find_element(By.XPATH, "//button[@id='btn_16']").click()
time.sleep(30)
driver.quit()
print("Page is ready!")
except TimeoutException:
print("Loading took too much time!")
CodePudding user response:
I suggest using a different xpath. Try using the href of the a
tag.
time.sleep(5)
element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(text(), '16. tokenURI')]")))
CodePudding user response:
Message: element click intercepted: Element <iframe width="100%" id="readcontractiframe" src="/readContract?m=normal&a=0xd0318da435dbce0b347cc6faa330b5a9889e3585&v=0xd0318da435dbce0b347cc6faa330b5a9889e3585" frameborder="0" scrolling="no" style="height: 1019px; padding: 0px 0.5px;" cd_frame_id_="5f818ce91d8292c77481277348168d1d"></iframe> is not clickable at point (674, 633). Other element would receive the click: <div role="alert">...</div>
(Session info: chrome=96.0.4664.110)
You had an element click interception deal with the popup first. Then proceed to click it as well.
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#btnCookie"))).click()
try:
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "readcontractiframe")))
elem=wait.until(EC.element_to_be_clickable((By.XPATH,"//a[contains(.,'tokenURI')]")))
elem.click()
print("Page is ready!")
except Exception as e:
print(str(e))
print("Loading took too much time!")