I am running into an issue where I am unable to select a button in the modal. The modal only appears when running my selenium script and does not appear when manually traversing the webpage. Interestingly enough the modal does consistently appear when my script runs try/except as shown below, but the program advances to the next step and is not able to get the element to click. Trying to get the element after this try:except also fails. The modal does not show up otherwise.
try:
element=driver.find_element_by_xpath('//div[contains(@href,"appointment")]')
element.click()
print("Looking for the modal button")
except NoSuchElementException:
print("*****Did not find the modal button")
I have tried the following with no success:
WebDriverWait(driver, 300).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-default btn-ok'][@data-dismiss='modal']"))).click()
This simply times out.Different methods to get the element:
element = driver.find_element_by_class_name("btn.btn-default.bt-ok")
and
element=driver.find_element_by_xpath('//div[contains(@href,"appointment")]')
In both cases the element is not found.
- Tried running with cookies, and the result is the same.
- Tried various sleep timers, but the modal does not appear during this sleep period
Here is the HTML Snippet of this section. Not sure if <div class =schedule-modal is a hint that there is some logic behind how/when the modal shows up?
<div class="schedule-modal modal fade in" tabindex="-1" role="dialog" aria-hidden="false" style="display: block;">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
<p><strong> Text
</p>
</div>
<div class="modal-footer">
<a class="btn btn-default btn-ok" href="/appointment">Start Appointment</a>
</div>
</div>
</div>
</div>
Thanks in advance for your help!
Update
Looks like the above code exists HTML code exists on the source page before the modal pops up which is why I was not able to access the element. My next question is what is this schedule-modal function doing:
function showNotice(dom) {
var $scheduleModal = $(".schedule-modal");
if ($scheduleModal.length) {
var link = $(dom).attr('href');
$scheduleModal.find('.btn-ok').attr('href', link);
$scheduleModal.modal('toggle');
return false;
}
CodePudding user response:
it looks like you are trying to find a div
element with an href
attribute containing appointment
. you'll want to select the a
element, which is the element that actually contains the reference you are trying to select. you have two options:
driver.find_element_by_xpath('//a[contains(@href,"appointment")]')
or
driver.find_element_by_xpath('//div/a[contains(@href,"appointment")]')
both will achieve the same results. but you may want to refine the selection just in case there are multiple a
tags that have an href
attribute contianing appointment
CodePudding user response:
If you are unsure if the modal element would appear or not you can wrap up the click to the modal button within a try-catch{}
block inducing WebDriverWait for the element_to_be_clickable()
and catching TimeoutException and you can use the following Locator Strategy:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
try:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[contains(., 'login or register')]"))).click()
print("Modal button clicked")
except TimeoutException:
print("Modal button was not found")