1
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.

vitaliis
  • 4,082
  • 5
  • 18
  • 40
mmsi
  • 13
  • 3
  • 2
    Check the answer and ✓(accept) it if everything works. I've tested it on my system and it works. – vitaliis Sep 09 '21 at 01:02
  • As per this post, https://meta.stackexchange.com/questions/75491/how-to-upload-an-image-to-a-post can you upload your images via a link if? –  Sep 15 '21 at 00:31
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 15 '21 at 03:31

3 Answers3

2

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()
vitaliis
  • 4,082
  • 5
  • 18
  • 40
1

So it looks like the readHeading6 isn't actually the button that you're trying to click.

If you drop down that div in inspect, it looks like the xPath you're looking to click is actually

/html/body/div[3]/div[6]/div[1]/a

If all you have to do is enter a uint64 and find out whether or not it's claimed (if I'm understanding the site correctly), I believe you may be able to ignore actually dropping down that menu and can possibly go straight to entering.

As an aside, if you're looking to see if it's claimed or not, you're gonna want to read the console output, here's some help

Depending on what you're trying to do, you don't actually need to click the button. I don't know about Chrome, but Firefox has a way to turn on "headless" mode, so the only output is in the terminal.

It's been a while since i've done it, but it should be something along the lines of

options = webdriver.FirefoxOptions()
options.set_headless()
browser = webdriver.Firefox(firefox_options = options)

Probably not of much help, but worth adding anyways.

1

Firstly, it may require to accept cookie that's why you can try on clicking "Got it" button as follows:

btn6 = browser.find_element_by_xpath('//*[@id="btnCookie"]') # error
btn6.click()
time.sleep(1.5)
Md. Fazlul Hoque
  • 15,806
  • 5
  • 12
  • 32