Cant find element with Selenium - python

I am trying to create a scraper for Etsy shop managers but after I log in I am unable to use the code to locate the shop manager button(NoSuchElement). Here is my code:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--incognito')
driverLocation = 'Downloads/chromedriver'
driver = webdriver.Chrome(driverLocation)
driver.get("https://www.etsy.com/signin")
username = driver.find_element_by_id("join_neu_email_field")
username.clear()
username.send_keys("username")
password = driver.find_element_by_id("join_neu_password_field")
password.clear()
password.send_keys("password")
driver.find_element_by_name("submit_attempt").click()
driver.implicitly_wait(10)
driver.find_element_by_class_name("wt-display-inline-flex-xs wt-flex-direction-column-xs wt-align-items-center wt-text-link-no-underline wt-p-xs-2 wt-pt-lg-0 wt-pb-lg-0 wt-pl-lg-1 wt-pr-lg-1 ge-nav-link reduced-margin-xs").click()
driver.quit()
The button I am trying to locate on the website:
I've tried various different methods(xpath, class_name, css_selector). I only see the class name as an identifier so I am not sure why its not working.

You should not use class name here because this class looks pretty much dynamic.
Using href attribute value in xpath is also not suggested.
In this case you should write an xpath with respect to some element which has static attribute or text. I feel there is a text in span tag so here you can try - //span[text()='putTheTextValue']/parent::a
Put a 5sec wait before interacting to this element just to make sure it is loaded when you are looking for this element. Later you can adjust this.

driver.find_element_by_class_name("wt-display-inline-flex-xs.wt-flex-direction-column-xs.wt-align-items-center.wt-text-link-no-underline.wt-p-xs-2.wt-pt-lg-0.wt-pb-lg-0.wt-pl-lg-1.wt-pr-lg-1.ge-nav-link.reduced-margin-xs").click()
Space in class shows that it's multiple classes you have to replace space with dot
Else use xpath or css
driver.find_element_by_xpath("//*[#class=\"wt-display-inline-flex-xs wt-flex-direction-column-xs wt-align-items-center wt-text-link-no-underline wt-p-xs-2 wt-pt-lg-0 wt-pb-lg-0 wt-pl-lg-1 wt-pr-lg-1 ge-nav-link reduced-margin-xs\"]").click()
driver.find_element_by_cssSelector("[class=\"wt-display-inline-flex-xs wt-flex-direction-column-xs wt-align-items-center wt-text-link-no-underline wt-p-xs-2 wt-pt-lg-0 wt-pb-lg-0 wt-pl-lg-1 wt-pr-lg-1 ge-nav-link reduced-margin-xs\"]").click()
This will select element that has attribute class with exact value

The <a> element have a child <span>. So to click on the element you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("a[href^='https://www.etsy.com/your/shops/me/dashboard'] > span").click()
Using xpath:
driver.find_element_by_xpath("//button[#class='save' and text()='save']").click()
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href^='https://www.etsy.com/your/shops/me/dashboard'] > span"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(#href, 'https://www.etsy.com/your/shops/me/dashboard')]/span"))).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

Css Selector button click with selenium (python)

I am trying to click on a button using selenium. My code states it is unable to find the css_selector with said class name. The class name has spaces in it, which lead me to use the css_selector object. When I try to pass the class name in the 'css_selector' object, it fails since the class name is a string, which is not callable. The website is password protected otherwise I would share the full code. This is what I have so far.
# Button I wish to click
See Full List
# Once button is clicked, it changes to:
See Full List
What I have tried:
driver.find_element(By.CSS_SELECTOR("btn btn-alt see-full-list-btn")) # str object not callable
driver.find_element(By.CSS_SELECTOR,"btn btn-alt see-full-list-btn")
# Message: no such element: Unable to locate element: {"method":"css selector","selector":"btn btn-alt see-full-list-btn"}
As you've mentioned
The class name has spaces in it, which lead me to use the css_selector
this is right approach, however you should also make sure that one
One should remove the space and put a .
. represent class in CSS.
So the below code should work:
driver.find_element(By.CSS_SELECTOR, ".btn.btn-alt.see-full-list-btn")
or you can even use it with the tag a
driver.find_element(By.CSS_SELECTOR, "a.btn.btn-alt.see-full-list-btn")
or the recommended solution would be to use with explicit waits:
see_full_list_button = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.btn.btn-alt.see-full-list-btn")))
see_full_list_button.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
There is no necessity to focus on the element HTML after the click is already invoked.
As per the HTML
See Full List
you can use either of the following locator strategies:
Using link_text:
driver.find_element(By.LINK_TEXT, "See Full List").click()
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "a.btn.btn-alt.see-full-list-btn").click()
Using xpath:
driver.find_element(By.XPATH, "//a[#class='btn btn-alt see-full-list-btn' and text()='See Full List']").click()
Ideally to click on the clickable element 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, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "See Full List"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-alt.see-full-list-btn"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='btn btn-alt see-full-list-btn' and text()='See Full List']"))).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

find_element() with Link_Text not working using Selenium Python

I am new to selenium whenever I try to find elements using id, name , xpath or anything it gives the same error
from selenium import webdriver
driver = webdriver.Safari()
driver.get('https://www.lambdatest.com/blog/selenium-safaridriver-macos/')
driver.find_element(By.Link_Text,'Webinar' )
It shows error by underlining By.
I tried writing
driver.find_element_by_id('Webinar')
But it does not go through I tried different websites and with different elements like class, id but seems like there is a problem with finding elements by any method is there something that needs to installed to make it work?
You have to take care of a couple of things here:
The supported locator strategy should be LINK_TEXT instead of Link_Text
The link_text should be Webinars instead of Webinar.
Effectively, your line of code should be:
driver.find_element(By.LINK_TEXT, 'Webinars' )
However, within the webpage there are multiple elements with innerText as Webinars. So you need to use a locator strategies which identifies the element uniquely within the DOM.
Solution
Ideally, to locate the clickable element from the menu container with text as Webinars you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.menu-menu-1-container a[href='https://www.lambdatest.com/webinar/']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='menu-menu-1-container']//a[text()='Webinars']")))
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
This
driver.find_element(By.Link_Text,'Webinar')
should be
driver.find_element(By.LINK_TEXT, 'Webinar')
Also, Import this from selenium.webdriver.common.by import By
If you are on Selenium4 then you should not really use:
driver.find_element_by_id('Webinar')
use this instead:
driver.find_element(By.ID, 'Webinar')
If you take a look at the source code, you'd see:
class By(object):
"""
Set of supported locator strategies.
"""
ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"

Click on ember.js enabled element using Selenium

I am trying to click on the following button on a linkedin page using selenium:
<button id="ember607" class="share-actions__primary-action artdeco-button artdeco-button--2 artdeco-button--primary ember-view" data-control-name="share.post"><!---->
<span class="artdeco-button__text">
Post
</span></button>
I have tried to use:
driver.find_element_by_id, but the id of the button seems to keep changing number
driver.find_element_by_xpath, but this contains the button number, so also fails
driver.find_element_by_class_name('share-actions__primary-action artdeco-button artdeco-button--2 artdeco-button--primary ember-view'), this fails even though the class name is correct ?
Basically, all methods generate the same error message:
Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element:{[*the_error_is_here*]}
I have also tried the xpath contains() method, but this does not find the button.
What would be the correct way to click on this button please ?
I am using python version 3.9 on windows with driver = webdriver.Chrome
The element is an Ember.js enabled element. So to click() on the element with text as Post you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("button.share-actions__primary-action[data-control-name='share.post']>span.artdeco-button__text").click()
Using xpath:
driver.find_element_by_xpath("//button[contains(#class, 'share-actions__primary-action') and #data-control-name='share.post']/span[#class='artdeco-button__text' and contains(., 'Post')]").click()
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.share-actions__primary-action[data-control-name='share.post']>span.artdeco-button__text"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(#class, 'share-actions__primary-action') and #data-control-name='share.post']/span[contains(., 'Post')]"))).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
References
You can find a couple of relevant detailed discussions in:
Selenium - Finding element based on ember
Automate Ember.js application using Selenium when object properties are changed at run-time
Ember: Best practices with Selenium to make integration tests in browser
Ember dropdown selenium xpath
Sometimes there are problems with buttons that are not clickable at the moment.
Try this:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 10)
button = wait.until(EC.element_to_be_clickable((By.XPATH, '[YOUR X_PATH TO THE BUTTON]')))
driver.execute_script("arguments[0].click()", button)
It's not the cleanest way to click any Button with selenium, but for me this method works mostly everytime.
//button[#class="share-actions__primary-action artdeco-button artdeco-button--2 artdeco-button--primary ember-view"].
Or
//button[contains(#id,'ember')]
Find the span with Post and click it's button tag.
//span[contains(text(), 'Post')]/parent::button
By xpath this should work:
//button/span[contains(text(), "Post")]
Combine it with a wait for the element:
button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button/span[contains(text(), "Post")]"))
)
The problem with your by class selectors is the multiple class names. See this question: How to get elements with multiple classes for more details on how to overcome that.

How to click on the span element using Selenium and Python

I'm trying to identify the following element but no matter the method it doesn't see it.
<span onclick="onClickTab('details'); return false;" id = "details" name = "details" class style ="display: inline;"">...</span>
I've tried with: Xpath, relative xpath, onclick, onclick contains, by id, by name, just nothing works.
It is a clickable button which appears after selecting an item in a list.
Current code is:
try:
WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"//span[#onclick='onClickTab('details'); return false;']")))
except
print("Error")
driver.find_element_by_xpath("//span[#onclick='onClickTab('details'); return false;']").click()
if there are any minor syntax problems like a "(" or such it might be because I typed it by hand, that shouldn't be the issue.
I'm forever grateful if you could point me to the right direction.
To click on the <span> element 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:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span#details[name='details'][onclick^='onClickTab']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#id='details' and #name='details'][starts-with(#onclick, 'onClickTab')]"))).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
I was able to select the element using the following xPath:
//span[contains(#onclick, "onClickTab(\'details\'); return false;")]
Using selenium, I used:
try:
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH,'//span[#onclick="onClickTab(\'details\'); return false;"]')))
except:
print("Error")
driver.find_element_by_xpath('//span[#onclick="onClickTab(\'details\'); return false;"]').click()
Here i suggest you to use id in Xpath.
driver.find_element_by_xpath("//span[contains(#id,'details')]").click()
if multiple element is there then you have to use foreach loop of driver.find_elements_by_xpath("//span[contains(#id,'details')]") and check with text and click on match element.

Python Selenium: can't figure out xpath to clickable link element in span tag

I'm using selenium python and have tried more than a dozen ways to find a clickable link element from a span tag but have been unsuccessful. I've tried using xpath, by link text, partial link text, and other suggestions researched.
The last 3 attempts using:
browser.find_element_by_link_text("Web Time Clock-Eastern").click()
element = browser.find_element_by_partial_link_text('Clock-Eastern')
browser.wait.until(EC.element_to_be_clickable((By.XPATH, '//a[#span]/html/body/div[6]/div[1]/div[1]/div[1]/div/div/div[4]/div[2]button'))).click()
I've provided image of the inspected element html below:
I expect to locate an element I can pass click method to open corresponding web page.
To click() the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.cardLinks a[href*='php/timeclock/WEB'] > span"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='cardLinks']//a[contains(#href, 'php/timeclock/WEB')]/span[text()='Web Time Clock-Eastern']"))).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
Try the below xpath.
//span[normalize-space(.)='Web Time Clock-Eastern']/parent::a
One of the main reasons for using XPath is when you don’t have a suitable id or name attribute for the element you wish to locate.
browser.find_element_by_xpath("//div[#class='cardLinks']/div/a").click()

Categories