'Selenium not finding element on XPATH on certain page
I want to press the Login Button on the site https://www.bet365.com/#/HO/ using selenium, I'm passing the XPATH of the Element that I select from inspect, but it doesnt work
text base HTML:
<divclass="hmMainHeaderRHSLoggedOutWide_Login">Login</div>
XPATH:
/html/body/div[1]/div/div[3]/div[1]/div/div[2]/div[4]/div[3]/div
Myscript.py
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
driver.get('https://www.bet365.com/#/HO/')
TIME_TO_LOAD = 10
try:
element = WebDriverWait(driver, TIME_TO_LOAD).until(
EC.presence_of_element_located(
(By.XPATH, '/html/body/div[1]/div/div[3]/div[1]/div/div[2]/div[4]/div[3]/div'))
)
finally:
driver.quit()
The button of the page that i want to click:

The text base HTML:< div class="hmMainHeaderRHSLoggedOutWide_Login">Login</ div>
Solution 1:[1]
Try the below xpath
"//*[normalize-space(text())='Login']"
Or
"//*[contains(text(),'Login')]"
to click on button
button = driver.find_element(By.XPATH, "//*[contains(text(),'Login')]")
button.click()
Also, provide the more info to understand the question clearly.
Solution 2:[2]
Always try to point out the elements you want using something that is most likely to stay the same every time you load the page, so the selector for the element you want should be using something unique, like "id" or "class", because the hierarchy is most likely to change.
In this case, I used the class "hm-MainHeaderRHSLoggedOutWide_Login" to locate the element.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
driver.get('https://www.bet365.com/#/HO/')
TIME_TO_LOAD = 10
try:
element = WebDriverWait(driver, TIME_TO_LOAD).until(
EC.presence_of_element_located(
(By.XPATH, '//*[@class="hm-MainHeaderRHSLoggedOutWide_Login "]')
)
)
print(element.text)
except Exception as e:
print(e)
finally:
driver.quit()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
