selenium cannot find element using python
You can directly use name
attribute, no need for the absolute xpath.
try this:
Css:
input[name='fsuser']
use it like this:
input_username = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[name='fsuser']")))
input_username.send_keys('some name')
OR
xpath:
//input[@name='fsuser']
use it like this:
input_username = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[@name='fsuser']")))
input_username.send_keys('some name')
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Update:
NoSuchElementException:
Please check in the dev tools
(Google chrome) if we have unique entry in HTML-DOM
or not.
xpath that you should check :
//input[@name='fsuser']
Steps to check:
Press F12 in Chrome
-> go to element
section -> do a CTRL + F
-> then paste the xpath
and see, if your desired element
is getting highlighted with 1/1
matching node.
If this is unique //input[@name='fsuser']
then you need to check for the below conditions as well.
Check if it's in any
iframe/frame/frameset
.Solution: switch to iframe/frame/frameset first and then interact with this web element.
Check if it's in any
shadow-root
.Solution: Use
driver.execute_script('return document.querySelector
to have returned a web element and then operates accordingly.Make sure that the element is rendered properly before interacting with it. Put some
hardcoded delay
orExplicit wait
and try again.Solution:
time.sleep(5)
orWebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='vaadin-text-field-container']//descendant::input[@part='value' and starts-with(@aria-labelledby, 'vaadin-text-field-input')]"))).send_keys("test")
If you have redirected to a
new tab/ or new windows
and you have not switched to that particularnew tab/new window
, otherwise you will likely getNoSuchElement
exception.Solution: switch to the relevant window/tab first.
If you have switched to an iframe and the new desired element is not in the same iframe context then first
switch to default content
and then interact with it.Solution: switch to default content and then switch to respective iframe.
Selenium cannot find element by ID nor xpath
Answering to close this question.
As Alok pointed out, we need to wait for the webelement to get loaded completely before trying to access it.
driver.get ("https://a website")
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "login-username")))
driver.find_element_by_id("login-username").send_keys(login)
Can't find element on webpage with Selenium
The element with the text as Logga in is within an <iframe>
so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using
CSS_SELECTOR
:driver.get("http://www.360functionalfitness.se/boka-pass/")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://crossfitmalmo.gymsystem.se/member#/schedule']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[ng-click^='showLogin'] span.ng-binding"))).click()Using
XPATH
:driver.get("http://www.360functionalfitness.se/boka-pass/")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://crossfitmalmo.gymsystem.se/member#/schedule')]")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@ng-click, 'showLogin')]//span[@class='ng-binding']"))).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 ECBrowser Snapshot:
Reference
You can find a couple of relevant discussions in:
- Ways to deal with #document under iframe
- Switch to an iframe through Selenium and python
- selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
- selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
Selenium webdriver can't find element sometimes
To find the search box on Tripadvisor instead of presence_of_element_located()
you have to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following Locator Strategies:
Using
CSS_SELECTOR
:searchbox = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='q'][title='Search']")))
Using
XPATH
:searchbox = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='q' and @title='Search']")))
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
selenium webdriver unable to find element from its xpath even though the xpath is correct
You need to just give wait to load the page. Your code is perfectly fine. Either give hardcode wait like sleep or presence of element. Both will work.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
driver = webdriver.Chrome(PATH)
driver.maximize_window()
wait = WebDriverWait(driver, 20)
driver.get('https://cq-portal.webomates.com/#/login')
wait.until(EC.presence_of_element_located((By.XPATH, "//button[@type='button']")))
element = driver.find_element(By.XPATH, "//button[@type='button']")
print(element.get_attribute('class'))
driver.quit()
Output:
btn btn-md btn-primary btn-block
selenium: Can not find element even with full xpath
You are missing a wait / delay.
You should let the page loaded before accessing the web element.
The best way to do that is to use expected conditions, as following:
from selenium import webdriver
from selenium.webdriver.chrome import service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
option = webdriver.ChromeOptions()
option.add_argument("start-maximized")
option.add_experimental_option("excludeSwitches", ["enable-automation"])
option.add_experimental_option('useAutomationExtension', False)
option.add_argument("--disable-blink-features")
option.add_argument("--disable-gpu")
#option.add_argument("--headless")
option.add_argument('--disable-blink-features=AutomationControlled')
wd = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=option)
url = "https://greyhoundbet.racingpost.com/#results-list/r_date=2021-01-01"
wd.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'})
wd.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
wd.get(url)
wait = WebDriverWait(wd, 20)
button = wait.until(EC.visibility_of_element_located((By.ID, "resultsDateBtn")))
#now you can click this button etc
button.click()
Related Topics
In Java, How to Combine Two Json Arrays of Objects
Java Nullpointerexception When Adding to Arraylist
Android - Running a Background Task Every 15 Minutes, Even When Application Is Not Running
Java.Sql.Sqlexception: Field Doesn't Have a Default Value
How to Count the Rows in Hibernate Query Language
How to Junit Test That Two List<E> Contain the Same Elements in the Same Order
Limiting the Number of Characters in a String, and Chopping Off the Rest
Spring Jpa/Hibernate Transaction Force Insert Instead of Update
Print Out All Possible Combinations of an Arraylist<Arraylist<String>> Recursively
How to Set a Maximum Height With Wrap Content in Android
Create List of Object from Another Using Java 8 Streams
Pass and Get Json String to Spring Controller
Java Properties - How to Create Dynamic Properties
How to Count Method Calls by Instance
Making a Mocked Method Return an Argument That Was Passed to It
Determine the Number of Pages in a Pdf File