How to click icon within pop-up in Selenium webdriver using Python - python

I'm trying to close credit card 3DS verification by clicking the 'X' icon at the top right of the pop-up as below.
CSS code for above pop-up is here:
<div id="three-ds-container" style="">
<i id="close-3ds" style="position: absolute;right: 10px;top: 10px;cursor:pointer;" class="fa fa-close"></i>
<iframe height="450" width="550" id="sample-inline-frame" name="sample-inline-frame"></iframe>
</div>
I tried to click the icon by using
driver.find_element_by_css_selector('div#three-ds-container i#close-3ds.fa.fa-close').click()
but the pop-up won't close.
I noticed that when I hover my mouse over the icon, the cursor will give indication that the icon can be clicked, is it related to my problem?
Thanks for the answer.

To click on 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#three-ds-container > i.fa-close#close-3ds"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='three-ds-container']/i[#class='fa fa-close' and #id='close-3ds']"))).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

The popup is in a different frame than you are, so I believe your issue is that you need to switch to that frame (iframe height="450" width="550" id="sample-inline-frame" name="sample-inline-frame")in order to click the close button.
The code to do this would look something like this:
iframe = driver.find_element_by_id("sample-inline-frame")
driver.switch_to.frame(iframe)
Then you should be able to do:
driver.find_element_by_css_selector('div#three-ds-container i#close-3ds.fa.fa-close').click()
I ran into the same issue previously, so I know the frustration, hope this helps.

Related

Cannot click tag <a> with <i class> selenium python

I want to make a simple attendance automation with python for my college, I make this for ease, because this took so many button to click but, I cannot click the tag <a> with tag <i> inside it.
<a href="http://siakad.polinema.ac.id/mahasiswa/tr_absensi/add"
class="btn btn-sm green-meadow btn-add-data" id="btn-add-wizard">
<i class="fa fa-plus"></i> Absen</a>
I use this wait function, and still not directed into that link. It shows nothing on result. Process finished with exit code 0
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//a[contains(#href, ""'http://siakad.polinema.ac.id/mahasiswa/tr_absensi/add')]"))).click()
I tried using ID and LINK_TEXT and still showing Process finished with exit code 0.
Thank you, apologize for my english. Enghlish is not my main language. If you need more information about my question, please let me know.
To click on the element with text as Absen 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, "a.btn.btn-sm.green-meadow.btn-add-data#btn-add-wizard[href$='id/mahasiswa/tr_absensi/add']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='btn btn-sm green-meadow btn-add-data' and #id='btn-add-wizard'][contains(#href, 'id/mahasiswa/tr_absensi/add')]"))).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

Selecting a submit button with Python (Selenium)

I want to automate a Github repository with Python (Selenium)while I use cmd.
I got to the last step: 'Create a new repository' on Github, but can't let python click on "Create repository".
Thanks for every help.
I have tried:
searchBar = driver.find_elements_by_css_selector('button.first-in-line').click()
and
searchBar = driver.find_elements_by_css_selector('button.first-in-line').submit()
<button type="submit" class="btn btn-primary first-in-line" data-disable-with="Creating repository…">
Create repository
</button>
I expect that python automatically clicks on the "Create repository" submit button, to finish the new git repository.
When you use find_elements_by_css_selector it will return a list.Instead of find_elements_by_css_selector you must use find_element_by_css_selector
driver.find_element_by_css_selector('button.first-in-line').click()
However if you want to use find_elements_by_css_selector then you should use index to get the first match and then click like below code.
driver.find_elements_by_css_selector('button.first-in-line')[0].click()
To click() on the element with text as Create repository 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, "button.btn.btn-primary.first-in-line"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-primary first-in-line']"))).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 this,
searchBar = driver.find_elements_by_css_selector('.button.first-in-line').click()
One thing, always try to use driver.find_elements_by_xpath() which help you to minimize lot of errors.

Python Selenium: Having trouble fixing NoSuchElementException for a specific button

I'm trying to press a button with selenium in python. I am not able to locate it with xpath or css selector.
For the rest of the things in the script I have been using xpath and it works fine, but this button's xpath seems to be relative each time I open it. Therefore I have tried to access is with the css selector.
The element I am trying to access looks like this:
<button type="button" data-dismiss="modal" class="btn btn-primary pull-right">Opret AnnonceAgent</button>
The css selector from inspect in chrome:
#\35 d167939-adc2-0901-34ea-406e6c26e1ab > div.modal-footer > div > button.btn.btn-primary.pull-right
Let me know if I should post more html.
I have tried many stack oveflow questions, and tried many variatiosn of the css selector, like putting it as:
driver.find_element_by_class_name('#\35 d167939-adc2-0901-34ea-406e6c26e1ab.div.modal-footer.div.button.btn.btn-primary.pull-rightdiv.button.btn.btn-primary-pull-right').click()
driver.find_element_by_class_name('div.button.btn.btn-primary-pull-right').click()
driver.find_element_by_class_name('button.btn.btn-primary-pull-right').click()
driver.find_element_by_class_name('btn.btn-primary-pull-right').click()
driver.find_element_by_class_name('btn-primary-pull-right').click()
I have also tried a sleep timer.
The button is in a window that opens when you press the previous button, with the background greyed out, if that helps. Picture.
# This opens up the window in which to press the next button (works fine)
button = driver.find_element_by_xpath('//*[#id="content"]/div[1]/section/div[2]/button')
button.click()
driver.implicitly_wait(15)
time.sleep(5)
# This is what doesn't work
driver.find_element_by_class_name('button.btn.btn-primary-pull-right').click()
I expect for the program to press the button and it doesn't, it just sits there.
The desired element is within a Modal Dialog Box, so you need to induce WebDriverWait for the desired 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.btn.btn-primary.pull-right[data-dismiss='modal']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-primary pull-right' and #data-dismiss='modal'][text()='Opret AnnonceAgent']"))).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
class_name recives one class as parameter, but you are using css syntax as locator.
With css_selector
driver.find_element_by_css_selector('button.btn.btn-primary-pull-right')
Which means <button> tag with 2 classes, btn and btn-primary-pull-right
With class_name
driver.find_element_by_class_name('btn-primary-pull-right')
My guess was right.There was an iframe which stopping to access the element.You have to switch to iframe first to access the button element.Try now with following code.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
#WebDriverWait(driver,20).until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH,'//iframe[starts-with(#id,"rdr_")]')))
WebDriverWait(driver,20).until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH,'//iframe[#id="qualaroo_dnt_frame"]')))
driver.find_element_by_css_selector('button.btn.btn-primary').click()

Python selenium webdriver locate and click links

I have a table column with four links in the same cell:
<td>
<a href="", onclick=showPricing('1234567','P', 'xyz123456', )>pdf,</a>
<a href="", onclick=showPricing('1234567','C', 'xyz123456', )>csv,</a>
<a href="", onclick=showPricing('1234567','X', 'xyz123456', )>xls,</a>
<a href="", onclick=showPricing('1234567','XP', 'xyz123456', )>xlsp</a>
</td>
I want to click the fourth one link text "xlsp", but not able to.
What I tried:
1) driver.find_element_by_xpath('//a[contains(#onclick, "xyz123456")]').click()
this of course clicks the first one: pdf
2) driver.find_element_by_xpath('//a[contains(#onclick, "xyz123456")][contains(text(), "xlsp")]').click()
this picks the fourth one, but error out with
"....errorElement could not be scrolled into view"
3) driver.find_elements_by_xpath('//a[contains(#onclick, "xyz123456")]')[3].click()
this returns the same error as 2)
Any suggestions are appreciated.
To click the fourth forth link with text as xlsp you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "td a[onclick*='XP']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#onclick,'XP') and contains(.,'xlsp')]"))).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 tested multiple options, including #DebanjanB's suggestion, and think this is the most reliable solution:
driver.execute_script(dr.find_element_by_xpath('//a[contains(#onclick, "xyz123456")][contains(#onclick, "XP")]').get_attribute("onclick"))

How to click on angular dropdown menu in Selenium

I am trying to use python selenium to automate some report generating on a website, however I am struggling to identify the element I need to click on the page due to the javascript. In firefox there is a DOM Event icon when I inspect the element. I have tried alot of variations including xpath etc but no luck.
<account-groups ng-if="EventsList.ToggleService.accountGroup();">
<button class="btn-default" ng-disabled="AccountGroupsCtrl.isDisabled()" ng-click="AccountGroupsCtrl.toggleFlyout()">
</button>
</account-groups>
EDIT For anyone viewing this the issue was because I had not switched frames using driver.switch_to.frame('frame_name'). Once this step is carried out the rest of the solutions below worked in identifying the elements. Thanks
As per the HTML you have shared it is not clear if the WebElement is a Dropdown. However as the desired element is an Angular element to invoke click() on the element you need to induce WebDriverWait and you can use either of the following solutions:
CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn-default[ng-click^='AccountGroupsCtrl']"))).click()
XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn-default' and starts-with(#ng-click,'AccountGroupsCtrl')]"))).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
You can use following code for selecting from dropdown.
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_id('*****'))
select.select_by_visible_text('****')

Categories