I'm having an issue clicking on an image using the Chromedriver with Selenium for the following HTML:
<div class="modal_buttons">
<input type="image" name="GoButton" id="GoButton" tabindex=14 title="Continue" alt="Continue" onfocus="SetLastFocus(this.id)" src="https://t4.ftcdn.net/jpg/00/81/83/05/240_F_81830511_aJbF2vH9yufF0UAUFQ83JDnbp0jE5mNV.jpg"
I tried using the following code:
element = driver.find_element_by_css_selector("//div[img/#src='https://t4.ftcdn.net/jpg/00/81/83/05/240_F_81830511_aJbF2vH9yufF0UAUFQ83JDnbp0jE5mNV.jpg']").click()
Selenium is failing everytime, and giving the same list of errors that it can't locate the button. Any ideas how to fix?
Thanks
Try:
driver.find_element_by_xpath("//input[#id='GoButton'][#name='GoButton']").click()
or
driver.find_element_by_xpath("//input[#id='GoButton'][#title='Continue']").click()
or
driver.find_element_by_xpath("//input[#name='GoButton'][#title='Continue']").click()
or
driver.find_element_by_xpath("//input[contains(#src,'240_F_81830511_aJbF2vH9yufF0UAUFQ83JDnbp0jE5mNV.jpg')]")
Related
I have developed an application using Angular, Node.js/Express.js and MySQL. The application has a login page that is displayed when any user visits the link. After logging in, the user is taken to the home page with a lot of other pages that are displayed in the navbar. I am trying to perform an automation test on the application and I am getting this error when I try to make my automated test click on one of the links in the navbar.
TypeError: 'WebElement' object is not subscriptable
After trying different things the error still does not get resolved. I ended up getting an error of Message: element not interactable
I have found the element using the console in Chrome and it is also clickable when I perform the click action in the console $x("//a")[1].click() or $$("[class = 'navbar-nav ml-auto'] li a")[1].click() but when I run the script in selenium then it throws the error. Can anybody tell me how can I make the navbar item clickable in selenium?
Here's my code:
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('http://name-of-the-local-server:3000')
browser.implicitly_wait(3);
email_element = browser.find_element_by_css_selector("input[name = 'usermail']")
password_element = browser.find_element_by_css_selector("input[name = 'passcode']")
submit_btn_element = browser.find_element_by_css_selector("button[name = 'loginButton']")
email_element.send_keys("someone#example.com")
password_element.send_keys("pass123")
submit_btn_element.click()
#executes perfectly until here
navbar_element = browser.find_elements_by_css_selector("[class = 'navbar-nav ml-auto'] li a")
navbar_element[1].click()
EDIT: I have added a bit of HTML that I want to click as by default it loads the home page
<a _ngcontent-imt-c24="" routerlinkactive="active" routerlink="/admin"
class="nav-link" href="/admin"><i _ngcontent-imt-c24="" class="bi bi-people"
style="font-size: 1.3rem;"></i> Admin</a>
<i _ngcontent-sun-c24="" class="bi bi-people" style="font-size: 1.3rem;"></i>
Any help would be appreciated. Thanks!
Instead of this :
navbar_element = browser.find_element_by_xpath("//a")
use this
navbar_element = browser.find_elements_by_xpath("//a")
find_element will return a single web element where as find_elements will return list of web elements
You are using navbar_element[0].click() if stored in list, it will click on first web element.
or iterate over a list like this :
for link in navbar_element:
ActionChains(driver).move_to_element(link).click().perform()
This question already has an answer here:
Selenium "selenium.common.exceptions.NoSuchElementException" when using Chrome
(1 answer)
Closed 2 years ago.
I'm trying to scrape the promotion information of each product from a website by clicking on the product and go to its detailed page. When the spider clicks on the product, the web will ask it to log in, and I tried the following code:
def __init__(self):
self.driver = webdriver.Chrome(executable_path = '/usr/bin/chromedriver')
...
def start_scraping(self, response):
self.driver.get(response.url)
self.driver.find_element_by_id('fm-login-id').send_keys('iamgooglepenn')
self.driver.find_element_by_id('fm-login-password').send_keys('HelloWorld1_')
self.driver.find_element_by_class_name('fm-button fm-submit password-login').click()
...
However, there is NoSuchElementException when I run it.
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="fm-login-id"]"}
'spider_exceptions/NoSuchElementException': 14,
The HTML of the login page is as follows:
<div class='input-plain-wrap input-wrap-loginid'>
<input id='fm-login-id' class='fm-text' name='fm-login-id'...>
event
</div>
So, I'm pretty sure the id should be 'fm-login-id'. The reason I could think of that might cause this issue is that this login page is a popup.
Basically, it pops up in the middle of the main page. Looking at the HTML of the site, I can see that the login type seems to be a new HTML window
<!DOCTYPE html>
<html>event
....
<\html>
I'm not sure if this is the issue, and if so, how to fix it? Also, is there other reasons that might've caused the issue?
The popup will have an ID. You might have to add f'#{popup_id}' to the end of response.url. Like this URL: https://stackoverflow.com/questions/62906380/nosuchelementexception-when-using-selenium-python/62906409#62906409. It contains #62906409 because 62906409 is the ID of an element in the page.
The login page inside a frame, you need switch it first:
#switch it first
self.driver.switch_to.frame(driver.find_element_by_id('J_loginIframe'))
self.driver.find_element_by_id('fm-login-id').send_keys('iamgooglepenn')
self.driver.find_element_by_id('fm-login-password').send_keys('HelloWorld1_')
And for login button you can't use .find_element_by_class_name, this method just for single class name. This element having multiple class name, so use .find_element_by_css_selector like bellow:
#submit button
self.driver.find_element_by_css_selector('.fm-button.fm-submit.password-login').click()
The login content seems to be nested in an iFrame element (if you trace it all the way to the top, you should find an iFrame with id="sufei-dialog-content"), which means you need to switch to that iFrame for that nested html before selecting your desired element, otherwise it will not work.
First you will need to use driver.switch_to.frame("sufei-dialog-content"), and then select your element with driver.find_element_by_name() or whatever you had.
A similar issue can be found here: Selenium and iframe in html
Just a simple mistake:
<div class='input-plain-wrap input-wrap-loginid'>
<input id='fm-login-id class='fm-text' name='fm-login-id'...>
event
</div>
is actually supposed to be:
<div class='input-plain-wrap input-wrap-loginid'>
<input id='fm-login-id' class='fm-text' name='fm-login-id'...>
event
</div>
You forgot a single-quote.
Have you tried driver.find_element_by_name('fm-login-id')?
You should try finding the elements by their XPaths. You just have to inspect the element, right-click on it and copy its XPath. The XPath of the first <input ... is //*[#id="fm-login-id"].
I am trying to create an automated login system for my local library website (http://mcls.ent.sirsi.net/client/en_US/mclweb) using Python and Selenium. However, my script is unable to find the username box.
The HTML from the website for the username box looks like this
<input maxlength="30" class="user_name_input" id="j_username" name="j_username" type="text">
and this is the code I used to find it
username = browser.find_element_by_id('j_username')
username.send_keys(u)
however, I am getting the following error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"j_username"}
Am I supposed to use a different function? Or did I use find_element_by_id() wrong? Thanks in advance.
The login form lies on a frame. You have to switch to the frame.
browser.get("http://mcls.ent.sirsi.net/client/en_US/mclweb")
browser.find_element_by_class_name('loginLink').click()
time.sleep(5)
browser.switch_to.frame(1) //login iframe is the second frame in the page
time.sleep(5)
browser.find_element_by_id('j_username').send_keys(u)
Alternative way, in case you want to do without switching to frame:
browser.get("http://mcls.ent.sirsi.net/client/en_US/mclweb/search/patronlogin")
browser.find_element_by_id('j_username').send_keys(u)
U can also login to that site without using iframe
browser.get("http://mcls.ent.sirsi.net/client/en_US/login")
browser.find_element_by_id('j_username').send_keys(u)
Have this line in my html:
<a class="btn" onclick="return confirm('Are you sure you want to delete this post?')" href="#">
Try to switch to the pop-up window and click OK by using
driver.switchTo().alert().accept()
but it keeps giving me AttributeError: 'WebDriver' object has no attribute 'switchTo'. I also try to locate the element ID of the buttons of the pop-up window but I could not make it work. Any suggestions will be appreciated.
In Python you should use
driver.switch_to.alert.accept()
as switchTo() is Java method
I am little late to answer, It is java syntax dear and you use python,
use this code:
alert = driver.switch_to.alert()
alert.accept()
I am trying to click a button that brings up a dialogue box to select a file. inspecting the element it looks like its an input rather than a button. Either way I cannot click it with:
element = browser.find_element_by_id("fileupload")
element.click()
and
browser.find_element_by_id("fileupload").send_keys("\n")
Neither of which seem to work.
Here is what I see when I inspect that element on the page:
<span class="btn btn-success fileinput-button">
<span class="glyphicon glyphicon-upload"></span>
Select and Upload...
<input id="fileupload" name="upfile" accept=".xml" type="file">
</span>
Any assistance help or pointers would be appreciated!
Clicking on a file input usually triggers a file upload dialog. Since you cannot control it with selenium, you need to avoid the dialog being opened by sending keys to the input instead:
browser.find_element_by_id("fileupload").send_keys("path_to_the_file")
See also:
How to deal with file uploading in test automation using selenium or webdriver
How to upload file using Selenium WebDriver in Java
How to upload file ( picture ) with selenium, python