0

I have to first select Unsubscription, then click Continue. enter image description here

I tried to find by Selector, xpath, and execute raw javescript. However, after clicking the button Continue, the page does not load the expected information. I saw the button color changed from gray to orange.

Below is my current code

unsubscripeButton = self._driver.find_element_by_css_selector('#actionType3')
ActionChains(self._driver).move_to_element(unsubscripeButton).perform().click()
continueButton = self._driver.find_element_by_css_selector('#pwt_form_Button_0')
ActionChains(self._driver).move_to_element(continueButton).click(continueButton).perform()

And this is the HTML code responsible for the Continue button. enter image description here

This is the Unsubscription and continue button after `Unsubscription is clicked:

<tr>        
    <td class="tableform" nowrap>
        <input id="actionType3" name="actionType" type="radio" value="U"/>Unsubscription
    </td>
</tr>   

and the Continue button is like this

<tr class="buttonmenubox_R">
    <td valign="top" align="right">
        <div type="submit" dojoType="pwt.form.Button" name="_eventId_continue" value="Continue" class="button">
        </div>
    </td>
</tr>
JOHN
  • 1,411
  • 3
  • 21
  • 41

2 Answers2

0

Possibly you are invoking click() even before the element is properly loaded i.e. JavaScript / AJAX calls are completed.

You need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Code Block:

    unsubscripeButton = WebDriverWait(self._driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#actionType3")))
    ActionChains(self._driver).move_to_element(unsubscripeButton).click(unsubscripeButton).perform()
    continueButton = WebDriverWait(self._driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "tr.buttonmenubox_R div.button[name='_eventId_continue'][value='Continue']")))
    ActionChains(self._driver).move_to_element(continueButton).click(continueButton).perform()
    
  • 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
    from selenium.webdriver.common.action_chains import ActionChains
    

Update

As the element is still not clickable with WebDriverWait and Actions as an alternative you can use execute_script() method as follows:

unsubscripeButton = WebDriverWait(self._driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#actionType3")))
ActionChains(self._driver).move_to_element(unsubscripeButton).click(unsubscripeButton).perform()
driver.execute_script("arguments[0].click();", WebDriverWait(self._driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "tr.buttonmenubox_R div.button[name='_eventId_continue'][value='Continue']"))))
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I added the `element_to_be_clickable`. The script completed without errors. However, the page still does not load as expected. The button changed from `grey` to `orange`. – JOHN Jan 14 '20 at 06:51
  • @JOHN I made a small change in the locator strategy, did you consider that in your test? – undetected Selenium Jan 14 '20 at 06:52
  • I tried changed to CSS selector to yours: `"span.button#pwt_form_Button_0>a.button#pwt_form_Button_0"` and I got timeout error. Then I tried to use the chrome console to locate the element. I got null. – JOHN Jan 14 '20 at 07:02
  • is `"span.button > a.button#pwt_form_Button_0"` what you mean? But I have also tried this... the same result. The button turned Orange. But didn't load. – JOHN Jan 14 '20 at 07:08
  • @JOHN Yeah, but does the previous line of code clicks on the `Unsubscription` button at all? Can you update the question with the text based HTML of the `Unsubscription` button and of `Continue` button once the `Unsubscription` button is already clicked? – undetected Selenium Jan 14 '20 at 07:15
  • Yes. The unsubscription button is clicked. – JOHN Jan 14 '20 at 07:18
  • Take a look on the HTML on `Continue`. It looks different in the inspect window – JOHN Jan 14 '20 at 07:25
  • @JOHN Checkout the updated answer and let me know the status – undetected Selenium Jan 14 '20 at 07:33
  • @JOHN Checkout the updated answer and let me know the status – undetected Selenium Jan 14 '20 at 08:01
  • Still timeout error. It seems that the element cannot be found. I tried in the Console. But I am not sure why there is a difference between source code and inspect element. – JOHN Jan 14 '20 at 08:05
  • @JOHN Can you find the element with the locator within my answer within Inspect Element? – undetected Selenium Jan 14 '20 at 08:07
  • No. I tried ```document.querySelector("tr.buttonmenubox_R div.button[name='_eventId_continue'][value='Continue']")``` return `Null` – JOHN Jan 14 '20 at 08:08
  • Hmm, possibly the HTML you provided is incorrect, please cross check – undetected Selenium Jan 14 '20 at 08:10
  • @JOHN Checkout the updated answer and let me know the status. – undetected Selenium Jan 14 '20 at 08:48
0
 public void waitForElementClickable(By locator) {
         Webdriverwait wait = new WebdriverWait(driver,30);
        try {
            wait.until(ExpectedConditions.elementToBeClickable(locator));
        } catch (Exception e) {

            System.out.println("Some error/exception while waiting for element to click -> " + locator.toString());
            e.printStackTrace();
            }

pass the locator as a parameter like this and wait till the element is clickable.. call this method before clicking , so that it would wait until the element is clickable and perform click operation.