Lord sama
Lord sama

Reputation: 31

how to click <a> in a webpage without class name or id or with python selenium

how to click a button in a webpage without class name or id, this is the web code

<a href="https://example.com" style="text-decoration:none;line-height:100%;background:#5865f2;color:white;font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:15px;font-weight:normal;text-transform:none;margin:0px;" target="_blank">
            Verify Email
          </a>

I tried this code

driver.find_element_by_xpath('/html/body/div/div[2]/div/table/tbody/tr/td/div/table/tbody/tr[2]/td/table/tbody/tr/td/a').click()

but it show this in the log

Message: no such element: Unable to locate element:

and I tried this too

driver.find_element_by_link_text('Verify Email').click()  

and it show this in the log

Message: no such element: Unable to locate element:

and I tried to add time.sleep(5) before it but the same problem

Upvotes: 0

Views: 135

Answers (2)

cruisepandey
cruisepandey

Reputation: 29362

looks like there are spaces, so can you try with partial_link_text as well :-

driver.find_element_by_partial_link_text('Verify Email').click()  

or with the below xpath :

//a[@href='https://example.com']

or

//a[contains(@href,'https://example.com')]

or

//a[contains(text(),'Verify Email')]

Update 1 :

There is an iframe involved, so driver focus needs to be switch to iframe first and then we can click on Verify Email, see below :-

Code :

driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://www.gmailnator.com/sizetmp/messageid/#17b0678c05a9616e")
driver.execute_script("var scrollingElement = (document.scrollingElement || document.body);scrollingElement.scrollTop = scrollingElement.scrollHeight;")
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "message-body")))
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(),'Verify Email')]"))).click()

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Upvotes: 1

Prophet
Prophet

Reputation: 33361

Try this XPath locator:

//a[contains(text(),'Verify Email')]

Or this:

//a[@href='https://example.com']

So your code will be

driver.find_element_by_xpath("//a[contains(text(),'Verify Email')]").click() 

or

driver.find_element_by_xpath("//a[@href='https://example.com']").click() 

Also possibly you have to add a wait to let the element loaded.
In this case your code will be:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 20)

wait.until(EC.visibility_of_element_located((By.XPATH, "//a[contains(text(),'Verify Email')]"))).click()

or

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 20)

wait.until(EC.visibility_of_element_located((By.XPATH, "//a[@href='https://example.com']"))).click()

Upvotes: 0

Related Questions