Python Selenium: Having trouble fixing NoSuchElementException for a specific button - python

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()

Related

Clicking a button with Selenium button

I'm trying to click on "Agree" button on this website https://www.soccerstats.com/matches.asp?matchday=1# but it didn't work for me using this code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
s=Service("C:/Users/dhias/OneDrive/Bureau/stgg/chromedriver.exe")
driver=webdriver.Chrome(service=s)
driver.get("https://www.soccerstats.com/matches.asp?matchday=1#")
driver.maximize_window()
time.sleep(1)
driver.find_element(By.CLASS_NAME," css-47sehv").click()
the css-47sehv is the class name of the button and here is a picture of the button The blue button
Though the element AGREE contains the classname css-47sehv, the value looks dynamic and may change in a short interval or once the application gets restarted.
Solution
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:
driver.get("https://www.soccerstats.com/matches.asp?matchday=1#")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[mode='primary']"))).click()
Using XPATH:
driver.get("https://www.soccerstats.com/matches.asp?matchday=1#")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#mode='primary' and text()='AGREE']"))).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 have to make sure of following:
1-use explicitly Wait to wait the button to/Till appear
try:
element=WebDriverWait(driver,10).until(
EC.presence_of_element_located((By.ID, "AgreeButton"))
)
finally:
driver.quit()
2-Click on the button with correct Xpath:
driver.find_element(By.XPATH,"//button[text()='AGREE']").click()
3-If simple click doesn't work you can use JavaScript and perform methods to click.
Try using this
driver.find_element_by_class_name('css-47sehv').click()
on place of
driver.find_element(By.CLASS_NAME," css-47sehv").click()
To click on AGREE button use the following xpath to identify the element and click.
//button[text()='AGREE']
Code:
driver.find_element(By.XPATH,"//button[text()='AGREE']").click()
Or Use following css selector.
driver.find_element(By.CSS_SELECTOR,"button.css-47sehv").click()

How to click icon within pop-up in Selenium webdriver using 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.

How do I make Selenium Python click on a button element?

So I just started using Selenium for Python and I am trying to click on a button element that is buried into a few div elements; I've tried so many things but nothing works. Everything in the code works besides the last part which is waiting for the button to be clickable and then clicking it. I would greatly appreciate some help here, thanks. :)
HTML:
Code trials:
Error stacktrace:
To click on **Maybe Later** button.
Induce WebDriverWait() and element_to_be_clickable() and following XPATH or CSS Selector.
XPATH:
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//div[#class='modal-footer']//button[#Class='btn btn-danger x' and text()='Maybe Later']"))).click()
CSS Selector:
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.modal-footer button.btn.btn-danger.x[style='danger']"))).click()
You need to import following libraries.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
css selectors will become your best friend,
you should always look to add as many attributes as possible
maybe_later_css = 'button[class="btn btn-danger"]'
# type str, '<tag-name>[<attribute-name> = <attribute-value>]'
driver.find_element_by_css_selector(maybe_later_css).click()
follow this format for all elements, its superior and works as expected every time
the only complication is when there exists multiple buttons with them same class name, in which case you should find a different attribute to fill the [] brackets
The element with text as Maybe Later is within a Modal Dialog Box so to locate and click() on the element 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, "div.modal-footer#eFooter button.btn.btn-danger.x[style='danger']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='modal-footer' and #id='eFooter']//button[#class='btn btn-danger x' and #style='danger']"))).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

How to click on the button when the textContext contains leading and trailing white-space characters?

I'm trying to click on the following button using Selenium with python:
<button type="submit" tabindex="4" id="sbmt" name="_eventId_proceed">
Einloggen
</button>
This is just a simple button which looks like this:
Code:
driver.find_element_by_id('sbmt').click()
This results in the following exception:
selenium.common.exceptions.ElementNotInteractableException: Message:
Element <button id="sbmt" name="_eventId_proceed" type="submit">
could not be scrolledinto view
So, I tried scrolling to the element using ActionChains(driver).move_to_element(driver.find_elements_by_id('sbmt')[1]).perform() before clicking the button.
(Accessing the second element with [1] because the first would result in selenium.common.exceptions.WebDriverException: Message: TypeError: rect is undefined exception.).
Then I used
wait = WebDriverWait(driver, 5)
submit_btn = wait.until(EC.element_to_be_clickable((By.ID, 'sbmt')))
in order to wait for the button to be clickable. None of this helped.
I also used driver.find_element_by_xpath and others, I tested it with Firefox and Chrome.
How can I click on the button without getting an exception?
Any help would be greatly appreciated
To invoke click() on the element you need to first use WebDriverWait with expected_conditions for the element to be clickable and you can use the following solution:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='sbmt' and normalize-space()='Einloggen']"))).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

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