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()
Related
Probably a silly question, but I have spent a ridiculous amount of time trying to figure this out. I am building a scrapper bot using selenium in python, and I am just trying to click a button on a web page. The web page opens and resizes...
def initalize_browser():
driver.get("**website name**")
driver.maximize_window()
but I cannot get it to click a specific button. This is the buttons HTML code:
<button class="mx-auto green-btn btnHref" onclick="window.location ='/medical'" onkeypress="window.location='/medical'">
Medical and Hospital Costs
</button>
And this is my code:
click_button=driver.find_element(by=By.CLASS_NAME, value="mx-auto green-btn btnHref")
click_button.click()
This is the error I get for this code:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".mx-auto green-btn btnHref"}
I have tried out so many variations of this, including:
driver.find_element_by_xpath('//button[#class="mx-auto green-btn btnHref"]').click()
Where I get this error:
AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'
I have also checked to see if there are perhaps any other attributes with the same class name, but there is not. Any help would be super appreciated, thank you!
The method find_element_by_xpath is deprecated now. Use this line:
driver.find_element(By.XPATH, '//button[#class="mx-auto green-btn btnHref"]').click()
instead of:
driver.find_element_by_xpath('//button[#class="mx-auto green-btn btnHref"]').click()
And be sure you have this in imports:
from selenium.webdriver.common.by import By
The locator click_button=driver.find_element(by=By.CLASS_NAME, value="mx-auto green-btn btnHref") doesn't work because By.CLASS_NAME needs only one class name to find an element, but you gave it 3 class names. The html attribute class consists of a list of elements divided by space. So, in this html code
<button class="mx-auto green-btn btnHref" onclick="window.location ='/medical'" onkeypress="window.location='/medical'">
Medical and Hospital Costs
</button>
the attribute class has 3 class names mx-auto, green-btn and btnHref
You can't use all the 3 classes with By.CLASS_NAME but you can use all of them using the By.XPATH
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'm using Python and Selenium. My problem is that I can't switch on the modal that pops out and I can't click the buttons in it.
This is the elements of the modal:
This is my code:
minus the url of course
browser = webdriver.Chrome(executable_path="D:\\sasdsa\\automate\\chromedriver_win32\\chromedriver.exe")
user_name = browser.find_element_by_xpath("//input[#id='username']")
user_name.send_keys("test.employee")
##Password
pass_word = browser.find_element_by_xpath("//input[#id='password']")
pass_word.send_keys("123")
##log_in = browser.find_element_by_css_selector(".btn")
log_in = browser.find_element_by_xpath("//button[#class='btn btn-sm btn-primary btn-block']")
log_in.click()
##punch
#driver.find_element_by_id("//#id='product_view')
#To open the modal
punch_in = browser.find_element_by_xpath("//button[#class='btn btn-success btn-sm pull-right']")
punch_in.click()
#cant switch to the modal to access the button
browser.switch_to_frame("product_view")
punch_in2 = browser.find_element_by_xpath("//button[#id='save_me']")
punch_in2.click()
Delete the line below and it should work fine.
browser.switch_to_frame("product_view")
You don't need to do anything special here. A modal dialog like this is just HTML like any other HTML on the page. You access it just like you would anything else.
Having said that... if you click a button, etc. that launches the dialog, you will probably have to add a WebDriverWait to wait for the dialog to be visible before accessing elements inside it, etc.
Andersson and JeffC are right. I had a similar issue. I treated the modal window as something different, which didn't work by the way. At the end, I just treated it in the usual way. I simply added browser.implicitly_wait(60) after initiating the browser, and it worked.
What Jeff said worked for me. I had
<div class="ReactModalPortal">
I just added sleep for 3 seconds to see if it was working. It worked.
Then I used :
act = ActionChains(self.driver)
act.send_keys(Keys.TAB).perform()
act.send_keys(Keys.ENTER).perform()
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')]")
I am very new to Web UI automation, my queries might be very basic.
My UI automation requirement,I have to click on 'Allow' Pop up message. Could you please help me that How I can do this will work, I have given following try, However, unfortunately, these try did not work.
Following things could not worked. :
driver.find_element_by_css_selector('p.instructions.ALLOW').click();
##
#driver.find_element_by_css_selector('p.instructions').click()
document = 'ALLOW'
#driver.find_element_by_xpath("//*[normalize-space()='"+document+"']").click();
#driver.findElement_by_xpath("//span[contains(., \"" + document + "\")]").click();
#driver.find_element_by_xpath("//p[contains(text(),'ALLOW')]/span").click(); ##2
#driver.find_element_by_xpath("//span[contains(text(),'ALLOW')]").click(); ##2
#driver.find_element_by_xpath("//span[contains(text(),'ALLOW')]").click()
#driver.find_element_by_xpath("//span[contains(text(),'Allow')]").click()
#//a[contains(text(), 'Created By Me')]/span
#section.hidden.MouseAllowCameraView
#content = driver.find_element_by_css_selector('section.MouseAllowCameraView').click()
=========HTML Code ===========
<section class="MouseAllowCameraView hidden"><div class="background"></div>
<div class="page">
<div class="content">
<p class="instructions"><bdo dir="ltr">Activate your webcam by clicking <span>**ALLOW**</span><br>at the top of your browser window.</bdo></p>
</div>
<div class="webcam-arrow"></div>
</div>
</section>
===================End HTML Code
After checking the site, your question became more clear to me and its answer too.
Your Site has to be open only in Google Chrome, Once we click on Start Button it pops-up a browser pop-up which request a user to click on 'Allow' to proceed further,
Now here is the actual problem.
Selenium so far is not capable of automating the controls of the Browsers(Favorite bar, Address bar,Menu options) and the window which we get on clicking on 'Start'button is a part of the controls of our chrome browser.
To handle this situation you can use Some third part tools like Sikuli,AutoIT or ROBOT.
I'll suggest to go with Sikuli, with the help of this you will be able to click on Allow button.
Here is the complete Sikuli Link1 and Link2 tutorial.
Please let me know if any questions.