2

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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Bertil Johannes Ipsen
  • 1,656
  • 1
  • 14
  • 27

3 Answers3

1

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

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')
Guy
  • 46,488
  • 10
  • 44
  • 88
  • @BertilJohannesIpsen Please elaborate. What exactly happened? – Guy Apr 08 '19 at 11:13
  • Right, sorry! I got the same exception, the `NoSuchElementException: Message: no such element: Unable to locate element: {"method":"class name","selector":"btn-primary-pull-right"}`, and similarly for the other one. – Bertil Johannes Ipsen Apr 08 '19 at 11:25
  • @BertilJohannesIpsen Try adding [explicit wait](https://selenium-python.readthedocs.io/waits.html#explicit-waits). You should also check if the button is in ` – Guy Apr 08 '19 at 11:30
  • thanks, I've tried both of these already (`time.sleep(t)` as well as in the thread @KajulKundu) – Bertil Johannes Ipsen Apr 08 '19 at 11:35
0

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()
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • Are you able to make this word? When I implement it, I get a `TimeoutException`. – Bertil Johannes Ipsen Apr 08 '19 at 10:33
  • well do one thing try this `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()` – KunduK Apr 08 '19 at 10:37
  • Hm, for that one I get a `NoSuchElementException` for the button again. – Bertil Johannes Ipsen Apr 08 '19 at 10:40
  • can you do one thing instead of image can you share the html so that i can copy entire html and test on my laptop whats wrong? – KunduK Apr 08 '19 at 10:42
  • Is the same html you are looking for?I can't find any iframe here when i see the html however when you posted image it was showing iframe? – KunduK Apr 08 '19 at 10:55
  • Ah, right sorry, this is the page source, but the stuff we are working with is rendered after the javascript. Is there a way for me to get the html with selenium so I can send i to you? – Bertil Johannes Ipsen Apr 08 '19 at 10:58
  • Once you reach the page press F12 on chrome and get the html – KunduK Apr 08 '19 at 11:02