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.
Related
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
I'm trying to click on the radio button that comes from in MODAL but a timeout exception comes every time.
Here is the DOM element with a modal screenshot
I'm using the PAGE OBJECT MODEL design pattern and below is the code. I'm trying to click through the main input locator and also select the main class.
Explitroy wait:
def get_element_clickable(self, by_locator):
WebDriverWait(self.driver, 30).until(EC.element_to_be_clickable(by_locator))
Call the locator in function:
def get_select_radio(self):
return self.get_element_clickable(self.Select_radio_button_of_modal)
Also, try this:
def get_element_clickable(self, by_locator):
WebDriverWait(self.driver, 30).until(EC.visibility_of_element_located(by_locator))
Can someone please help suggest to me how to resolve this issue?
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(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.addRemoveEFAW#haveEfaw"))).click()
Using XPATH:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='addRemoveEFAW' and #id='haveEfaw']"))).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
PS: As you are using PAGE OBJECT MODEL the the within the respective PageObject.py page you have to define them accordingly.
I am attempting to click a button using an XPath but I am unable to locate the element. Complete noob here.
Here is the button element I copied:
<button _ngcontent-ygw-c218="" class="btn"><span _ngcontent-ygw-c218="" translate="">SHOW ALL</span></button>
Here is my programming:
ShowAll =driver.find_element_by_xpath('//*[#id="app-SelectComponents"]/div[1]/button[1]')
ShowAll.click()
I have tried the following solution I found online to no avail (I've also replaced the 'btn' with 'SHOW ALL', no luck there):
driver.switch_to.frame(driver.find_element_by_name('btn'))
ShowAll =driver.find_element_by_xpath('//*[#id="app-SelectComponents"]/div[1]/button[1]')
ShowAll.click()
driver.switch_to.default_content()
Much appreciated.
EDIT: Here is a picture for reference. What am I doing wrong or what I can do to work around this issue?
You need to add time to make sure the element is available before asking selenium to pick it.
Try this:
import time
driver.switch_to.frame(driver.find_element_by_name('btn'))
time.sleep(5) #you can change the 5 depending on the number that works
ShowAll =driver.find_element_by_xpath('//*[#id="app-SelectComponents"]/div[1]/button[1]').click()
driver.switch_to.default_content()
The element with the text as SHOW ALL 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:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#iParts")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn > span[translate]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='iParts']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn']/span[text()='SHOW ALL']"))).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
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
Hi I was trying to type the username field using Selenium and Python for the website https://mail.protonmail.com/create/new?language=en.
From the developer tool, I am able to inspect the item using CSSSelector/Xpath or other way. But when I am running the pthon script its not working. Screenshot attached:
My code is like the following:
BASE_URL = 'https://mail.protonmail.com/create/new?language=en'
driver = webdriver.Chrome(executable_path='./drivers/chromedriver')
driver.get(BASE_URL)
river.find_element_by_xpath('//*[#id="username"]').send_keys('someStringValue')
And after executing the following code, geetting the error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="username"]"}
(Session info: chrome=83.0.4103.97)
Any suggestion?
The Email Address field 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('https://mail.protonmail.com/create/new?language=en')
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"div.usernameWrap iframe[title='Registration form']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input#username"))).send_keys("FunnyBoss")
Using XPATH:
driver.get("https://mail.protonmail.com/create/new?language=en")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//div[#class='usernameWrap']//iframe[#title='Registration form']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='input' and #id='username']"))).send_keys("FunnyBoss")
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
Browser Snapshot:
Reference
You can find a relevant discussion in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
Your xpath is OK, but the forms are inside an iframe.
So you need to switch to the iframe first:
driver.switchTo().frame(n);
Edit: If you read the TOS, you will see
This Service is provided exclusively to persons. Accounts registered by “bots” or automated methods are not authorized and will be terminated.
I'm trying to find the element and click for the button "Not Now". I've tried with with css_selector, xpath, but I"m unable at all to find the proper way.
HTML:
To locate and click() on the element with text as Not Now you can use the following Locator Strategy:
Using xpath:
driver.find_element_by_xpath("//button[text()='Not Now']").click()
However, the element looks dynamic to me so you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategy:
Using XPATH:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div//button[text()='Not Now']"))).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
Reference
You can find a couple of relevant discussions in:
What does contains(., 'some text') refers to within xpath used in Selenium
While fetching all links,Ignore logout link from the loop and continue navigation in selenium java
How does dot(.) in xpath to take multiple form in identifying an element and matching a text