Issue
url = 'https://etherscan.io/token/0x7ae1d57b58fa6411f32948314badd83583ee0e8c#readContract'
browser = webdriver.Chrome('C:\webdrivers\chromedriver')
headers1 = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
}
# wait = WebDriverWait(browser, 10)
browser.get(url)
time.sleep(10)
btn6 = browser.find_element_by_xpath('//*[@id="readHeading6"]/a') # error
btn6.click()
StackOverflow won't let me post an image so I copied the element below.
<a class="btn btn-link btn-block text-dark d-flex justify-content-between align-items-center py-2 collapsed" data-toggle="collapse" href="#readCollapse6" aria-expanded="false" aria-controls="readCollapse6">6. claimedByTokenId <span class="accordion-arrow"><i class="fas fa-arrow-down small"></i></span></a>
Also, every XPath helper I've used failed to recognise the button I'm trying to locate. I've tried finding by CSS selector, class name, id, XPath, full Xpath. The XPath in the code above is from chrome. Inspect element > copy > xpath.
I'm fairly new to programming so any help is appreciated! Thanks.
Solution
Your XPath is correct, but you are missing few important things. The main is iframe. So, the correct steps for your task are:
1 Click "Got it" to accept the cookies.
2 Switch to iframe as your button is located inside iframe.
3 Import WebDriverWait to explicitly wait for your elements, instead of time.sleep
And your button will be clicked.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = 'https://etherscan.io/token/0x7ae1d57b58fa6411f32948314badd83583ee0e8c#readContract'
browser = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
headers1 = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
}
browser.get(url)
wait = WebDriverWait(browser, 10)
coockie_btn = wait.until(EC.element_to_be_clickable((By.ID, 'btnCookie'))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "readcontractiframe")))
btn6 = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="readHeading6"]/a'))).click()
Answered By - vitaliis
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.