Cannot click button - element not found - python

I have tried to make my script click the log in button on the Spotify login page. I have been searching all day for a solution, but no matter what class, css or whatever I put in, it is just saying it could not find the object.
My code:
import selenium
from selenium import webdriver
browser = webdriver.Firefox()
browser.get("https://accounts.spotify.com/en/login?
continue=https:%2F%2Fwww.spotify.com%2Fdk%2Faccount%2Foverview%2F")
username = browser.find_element_by_id("login-username")
password = browser.find_element_by_id("login-password")
username.send_keys("myUser")
password.send_keys("myPass")
login = browser.find_element_by_class_name('ng-pristine ng-valid-sp-
disallow-chars ng-invalid ng-invalid-required').click()
It is the last line I have a problem with. Note: This is just one of the options I tried.

You can try this code that have xpath:
login = browser.find_element_by_xpath("//button[contains(.,'Log In')]");
login.click();

You're using multiple classes in your class selector. Try using only one class. Something like this:
browser.find_element_by_class_name('btn-green').click()
If it still doesn't work, perhaps the element has not yet been rendered when you try to click it, so you may want to try with an explicit wait
Also, I checked out your selector and it looks like you weren't even pointing to the button, but to the form.
And finally, a class selector may not be the optimal way to locate your element here. I'd probably go with a XPath selector to find the button that contains the text Log In, using the following XPath
//button[text()='Log In']

Use this, i Updated the answer:
login = browser.find_element_by_xapth("//button[#class='btn btn-sm btn-block btn-green ng-binding']")
login .click()

browser.find_element_by_xpath(".//button[contains(text(), 'Log In'])").click()
This should work so give it a try. :)
As #Verv stated, you could be having timing issues. So just for the test try with time.sleep() to see if this is it.

Related

How click in Button with Selenium python

I want to click the configuration button, which leads to discord profile information,
Guys, I want to click on the gear at the bottom, how can I do this? This is my code:
option = Options()
option.add_argument("--no-sandbox")
driver = webdriver.Chrome(options=option)
driver.get("https://discord.com/channels/395582581124497408/395582581124497412")
wait = WebDriverWait(driver, 5)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a.item'))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#contents-18-Yxp"))).click()
Can anyone help me with this problem?
you can click it with this code:
driver.find_element_by_css_selector('div'[aria-label="User settings"]').click()
Find the ID of that button, then use driver.find_element_by_id("ID").click()
You should read this tutorial https://www.geeksforgeeks.org/click-element-method-selenium-python/
First, you must get an identifier for your element, for example:
class
xpath
css selector
... etc
then drive.find_element_by_(class, xpath, ...)(string).click()
Example:
driver.find_element_by_xpath('(//*[#class="contents-18-Yxp"])[7]')
this is the identifier for the settings section, you can copy it, note that this xpath can be changed if the discoed is updated, the solution is easy, just with the same way get the new xpath and paste it instead of the old one.
with the same Technique, find the user profile identifier, and paste it and remember .click()

How can I crawling on this page? I have a specific error

Question: I have the error (see in image below). I wanted to move on next page to
click button but code doesn't move on. How can I use this code?
This line is causing the error. It might be because of a wrong xpath.
driver.find_element_by_xpath("//a[#class='next']").click()
It might be easier to find the "next" element using an id or class like this-
driver.find_element_by_id("#element_id")
driver.find_element_by_css_selector("div .... a.next")
Also, the full xpath is something like this. Maybe try this once
//*[#id="react-root"]/div/div[2]/div/div[3]/section[4]/div[2]/a[12]
Invoking driver.execute seems to work due to overlapping element.
wait = WebDriverWait(driver,10)
for i in range(1, pageNum):
elem=wait.until(EC.presence_of_element_located((By.XPATH,"//a[#class='next']")))
driver.execute_script("arguments[0].click();", elem)
time.sleep(1)

python - having a hard time on selenium to click on button

I am working with selenium (python) and I would like for it to click on a button. The code goes something like this:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('my_url')
try:
driver.find_element_by_partial_link_text('Mais').click()
except:
pass
Nothing at all happens. After inspecting the source, and I am pretty sure this is the element I want to find:
<input class="ksb _kvc" value="Mais resultados" id="smb" data-lt="Carregando..." jsaction="str.smr" data-ved="0ahUKEwjPtK6Fwv3PAhVGI5AKHTkSDVsQxdoBCE8" type="button" />
Have you tried
driver.find_element_by_id('smb').click()
Rather than
driver.find_element_by_partial_link_text('Mais').click()
I suspect nothing happens because an exception is thrown in your original code if the find fails.
When attempting to locate elements I would usually use id/name as these are normally unique. For a full list see selenium-python.readthedocs.io/locating-elements.html
You are having issue clicking the button because you are using driver.find_element_by_partial_link_text() which is only good for finding link (anchor elements) by its partial text, and performing actions on it.
You should rely on the other find_element_by_xxxx() methods which allow us to find by element ID, Name, XPath, CSS selector, or Class name.
Refer the documentation on locating elements at : http://selenium-python.readthedocs.io/locating-elements.html

Using Python and Selenium I am unable to get to elements in overlaying model

In my test I have to click a button to add an item to the cart. At that point a model dialog comes up and allows some options and links, one of which I am trying to click; View Cart and Checkout.
Every attempt I make to find my element with find_element_by fails with a NoSuchElementException. Doesn't matter how I try to find it; ID, Class, Link Text, etc.
I have tried setting up WebDriverWait in a try/catch statement to see if pausing for the model to be added to the page would help, and it doesn't. I have also checked to see if there is another window or frame to switch to, but there is only the one window.
Are there any other ways to get what I need done, or troubleshooting steps that I can do to figure this out?
Below is what Firefox shows the HTML to be.
<div class="summary">
<dl></dl>
<a class="bb-button" href="/shopping/cart">View Cart and Checkout</a>
<div class="continue"></div>
The python I have written is:
product_details.addDocumentToCart('Printed Edition')
try:
WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(By.CLASS_NAME, 'bb-button'))
print 'bb-button found'
except:
print 'bb-button not found'
windows = self.driver.window_handles
print 'Windows after: ' + str(len(windows))
element = self.driver.find_element_by_class_name('bb-button')
element.click()
The messages I printed out return:
added_to_cart-container not found
and
Windows after: 1
The error I get when running this is:
NoSuchElementException: Message: Unable to locate element: {"method":"class name","selector":"bb-button"}
Thanks Mikko, with your advice about the pbd.set_trace() I was able to discover that my problem was really with the button that I thought was being clicked but wasn't. Further troubleshooting showed that my addDocumentToCart() method was clicking the wrong element, and so my model didn't come up, but no error was generated which lead me to believe that things were working when the really were not.

Selenium Python document.getElementsByClassName()

I'm using Selenium with Python3 to automate entering data into a website.
I have tried looking everywhere for how to deal with selecting an element by class if there is more than one but I can't figure out how to select the accordion-toggle[1]. Nothing happens on selenium but it works fine on any browser. Also, is there any way to just use the regular javascript or jquery commands?:
accordion=find_element_by_class("accordion-toggle"[1])
accordion.click()
#otheraccordion=find_element_by_css_selector("#AdvancedAddress > div.accordion-heading.in > div.accordion-toggle > span.accordionExpandCollapse")
#otheraccordion.click()
StreetNameField=driver.find_element_by_id("Advanced_FindMyParcel_PropertyStreetName")
StreetNameField.send_keys("Sherman")
ZipField=driver.find_element_by_id("Advanced_FindMyParcel_PropertyZip")
ZipField.send_keys("90201")
ZipButton=driver.find_element_by_id("btnSearchFindMyParcels")
ZipButton.click()
You actually can use document.getElementsByClassName() through execute_script() call:
driver.execute_script("document.getElementsByClassName('accordion-toggle')[0].click();")
But I would not go down to executing javascript for such a simple task. Easier to locate the element using find_element_by_class_name():
accordion = driver.find_element_by_class_name('accordion-toggle')
accordion.click()
You are looking for find_element(s)_by_css_selector - reference here - use the css prefix '.classname` to indicate the class.
e.g. to find
<div class='theClass'>
driver.find_elements_by_css_selector('.theClass')
You can also use the By syntax:
driver.find_elements(By.CSS_SELECTOR, '.theClass')
Edit
It seems the problem may be more to Clicking the element, rather than finding it.
Ensure the element is visible
For Chrome, you may need to mimic hovering the mouse over the element before clicking this - see Actions / ActionChains MoveToElement to hover over the element.
For IE, you may need to ensure the browser / frame gets the focus, prior to the element Click - you may need to apply a hack like one of these.
In addition to #alecxe response, I would also suggest using the function find_elements_by_class_name instead of find_element_by_class_name in case there are multiple elements with the same class name.
accordion = driver.find_elements_by_class_name('accordion-toggle')[1] # Selects second element
accordion.click()
Using find_element_by_class_name will return only the first element with that class name.

Categories

Resources