Find Element in Selenium Using Xpath or CSS Selector

Python selenium find element by xpath or css selector

Try this one, should work for you:

ele=driver.find_element_by_xpath("//*[@id='breadcrumb']//span")
ele.click()

Can i use xpath to find element that contains ::after?

It's impossible with XPath, at least with XPath version 1.0 supported by Selenium.

With selenium you can access pseudo elements ::before and ::after with CSS Selectors only.

See here and here for more detailed explanations

Locate web elements with JavaScript by CSS Selector or XPath?

document.querySelector() //for single node with css like path or selector

document.querySelectorAll() //for multiple nodes

To select by ID:

document.querySelector('#ID') //returns single element
document.querySelectorAll('#ID') //returns node list, you may need to use forEach

To select by class:

document.querySelector('.class') //returns single element
document.querySelectorAll('.class') //returns node list, you may need to use forEach

To select inside div by class:

document.querySelector('div > .class') //returns single element
document.querySelectorAll('div > .class') //returns node list, you may need to use forEach

Here is the documentation

Best way to get XPATH and CSS SELECTORS for scraping with Selenium

You are correct that right-clicking and Copy Xpath is a bad way to get an Xpath. You are left with a long and brittle selector. It is much better to build your own. Once you get the hang of it, it is pretty simple to start building your own CSS and Xpath selectors. Some of them get complicated but if you keep practicing and searching for solutions you will get better and better.

The problem is it is very difficult to explain how to do it in a forum like this. Your best bet is to YouTube some videos on how to create Xpath and CSS selectors for Selenium. Here is a decent one I just found for Xpath:

https://www.youtube.com/watch?v=3uktjWgKrtI

This follows the approach I use in Chrome Dev Tools and using the built in Find window (no plugins)

Here is a good cheatsheet I have used in the past for Xpath and CSS Selectors

https://www.automatetheplanet.com/selenium-webdriver-locators-cheat-sheet/

Good luck

Selenium can't find xpath or css selector

It might not work finding the element because it's not visible in UI - it is loaded but not visible, the easiest way is to move to that element and click on it.

next_button = driver.find_element_by_css_selector('[aria-label=\'Next\']')
actions = ActionChains(driver)
actions.move_to_element(next_button).perform()
next_button.click()

Selenium Driver - finding element with no ID, Name, Xpath, Tag, Class, CSS Selector

Try locating it like below:

input_field = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']")))
input_field.click()

You will also need the following imports:

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


Related Topics



Leave a reply



Submit