1

I'm trying to click on a button that expands on click but selenium is unable to locate the element. The element identified seems to be correct. I am using the Page Object Model here.

I tried to first simply find the element and click on it and then also tried to use ActionChains. Tried changing the element value and the methods of identifying such as ID, XPath, CSS Selector but nothing seems to work.

HTML:

<tr style="border-top:1px solid #e6e6e6;"><td style="display:inline-block;"><div class="expand"><i id="expand_2971740_2086074" class="fa fa-plus-circle" onclick="" style="display: block;"></i><i style="display: none;" id="collapse_2971740_2086074" class="fa fa-minus-circle" onclick="closeBundleCourses(2971740,2086074)"></i></div></td><td class="text-center">Course1Jan</td><td class="text-center">19-06-2019</td><td class="text-center">0</td><td class="text-center">1</td></tr>

Code trials:

click_plus_button = 'expand_2971740_2086074' #id

def __init__(self,driver):
        self.driver = driver

    def enroll_user(self,firstname,lastname,email):
        self.driver.find_element_by_link_text(self.go_to_manage_users).click()
        self.driver.find_element_by_class_name(self.expand_manage_users).click()
        time.sleep(2)
        actions = ActionChains(self.driver)
        actions.move_to_element(self.driver.find_element_by_id(self.click_plus_button)).click().perform()
        self.driver.find_element_by_id(self.manage_users_firstname).send_keys(manage_users_firstname)
        self.driver.find_element_by_id(self.manage_users_lastname).send_keys(manage_users_lastname)
        self.driver.find_element_by_id(self.manage_users_email).send_keys(manage_users_email)

Expected Result:

Expected Result

Actual Result:

Actual Result

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

1

The desired element is a dynamic element so to 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.expand i.fa.fa-plus-circle[id^='expand_']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='expand']//i[@class='fa fa-plus-circle' and starts-with(@id, 'expand_')]"))).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