Home > Software design >  Not able to click on "Home" Button Python
Not able to click on "Home" Button Python

Time:12-04

On Website when I login with Username: mercury and PW: mercury and click on Submit button I go to Login Successfully page. Then I need to click on home button which is on the left side but i can't.

from telnetlib import EC
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome(executable_path="D:\ChromeDriverExtracted\chromedriver")
driver.get("http://demo.guru99.com/test/newtours/")
driver.maximize_window()

driver.find_element_by_name("userName").send_keys("mercury")
driver.find_element_by_name("password").send_keys("mercury")
driver.find_element_by_name("submit").click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/table/tbody/tr/td[1]/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[1]/td[2]/font/a"))).click()

CodePudding user response:

This is an absolute xpath

/html/body/div[2]/table/tbody/tr/td[1]/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[1]/td[2]/font/a

please use one of the text-based locator from below:

//a[text()='Home']

or with LINK_TEXT like this :

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Home"))).click()

You can also use the below CSS selector:

a[href='index.php']

There are other ways as well to click,

Code trial 1:

time.sleep(5)
driver.find_element_by_xpath("//a[text()='Home']").click()

Code trial 2:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Home']"))).click()

Code trial 3:

time.sleep(5)
button = driver.find_element_by_xpath("//a[text()='Home']")
driver.execute_script("arguments[0].click();", button)

Code trial 4:

time.sleep(5)
button = driver.find_element_by_xpath("//a[text()='Home']")
ActionChains(driver).move_to_element(button).click().perform()

Imports:

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

CodePudding user response:

To click on the element with text as Home you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using LINK_TEXT:

    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.LINK_TEXT, "Home"))).click()
    
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.signin-btn.sbLoginBtn > span.btf-text"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Home']"))).click()
    
  • Note: You have to add the following imports :

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