0

I cannot click this element, tried CSS selector and Xpaths. Can anyone help, keep getting invalid selector, XPath/cannot locate element. Yet when I use the inspector to validate these elements they are correct, why can't my web driver script locate them?

HTML:

<div class="ap-ba-well-button"> 
    <!-- ngIf: service.booking_status_type !== 'Not Bookable Online' -->
    <button ng-if="service.booking_status_type !== 'Not Bookable Online'" class="ap-button ng-binding ng-scope">Book</button>
    <!-- end ngIf: service.booking_status_type !== 'Not Bookable Online' --> 
    <!-- ngIf: service.booking_status_type === 'Not Bookable Online' --> 
</div>

Code trials:

driver.find_element(By.CSS_SELECTOR, ".ap-popover-well-group-single:nth-child(1) .ap-button").click()
driver.find_element_by_xpath(“//?/button[@innertext='Book']”) 
driver.find_element_by_xpath(“/html//span[@class='ng-scope']/ap-booking-app[@class='ng-scope']/div[@class='ap-ba-wrapper ng-scope']/div[@class='ap-ba-container with-footer']//ap-booking-app-step-services[@data='data']/div/div[1]//div[@class='ap-ba-well-single ng-scope']//button[@class='ap-button ng-binding ng-scope']”)

any help will be great

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
3eternus
  • 33
  • 1
  • 1
  • 8

3 Answers3

0

The problem is the desired element is not present inside the webpage when you want to make a click, even if you have provided a correct path.

My way of going around this problem is like this:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as E

clickButton = WebDriverWait(browser,5).until(EC.element_to_be_clickable((By.XPATH, "ID")))
Stefan
  • 375
  • 1
  • 9
  • 24
  • seems common response by all, but this still isnt working.Going to try a robotframework – 3eternus Jan 07 '20 at 13:06
  • Well, I have faced this problem many times, and every time I used something new to overcome it, so do I bit research try out many implementations and find the best that suits your problem. – Stefan Jan 07 '20 at 21:40
0

From the HTML you have provided and your code trials it is pretty much evident that the element is an Angular element, so you have to induce WebDriverWait for the elementToBeClickable() and you the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.ap-ba-well-button button.ap-button.ng-binding.ng-scope"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='ap-ba-well-button']//button[@class='ap-button ng-binding ng-scope' and text()='Book']"))).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

Induce WebDriverWait() and element_to_be_clickable() and following Xpath Options.

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[text()='Book']"))).click()

OR

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[@class='ap-button ng-binding ng-scope' and text()='Book']"))).click()

You need to import following libraries.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • stil not workingthink this is an angular related problem im trying a robot framework – 3eternus Jan 07 '20 at 13:06
  • @3eternus : Share your url If possible.I believe the `book` button must be inside an `iframe`? Check your DOM if you find any `iframe` above `book` button? – KunduK Jan 07 '20 at 13:13