Нажал кнопку без ответа в Selenium - PullRequest
0 голосов
/ 14 января 2020

Я должен сначала выбрать Unsubscription, затем нажать Continue. enter image description here

Я пытался найти с помощью Selector, xpath и выполнить необработанный код JavaScript. Однако после нажатия кнопки Continue страница не загружает ожидаемую информацию. Я видел, как цвет кнопки изменился с серого на оранжевый.

Ниже приведен мой текущий код

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()

И это код HTML, отвечающий за кнопку Continue. enter image description here

Это кнопка Unsubscription и continue после нажатия кнопки `Unsubscription:

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

и кнопка Continue выглядит следующим образом

<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>

Ответы [ 2 ]

0 голосов
/ 14 января 2020
 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();
            }

передайте локатор как такой параметр и дождитесь, когда элемент станет кликабельным ... вызовите этот метод, прежде чем щелкнуть, чтобы он дождался кликабельности элемента и выполнил операцию щелчка.

0 голосов
/ 14 января 2020

Возможно, вы вызываете click() даже до правильной загрузки элемента, т.е. JavaScript / AJAX вызовы завершены.

Вам необходимо вызовите WebDriverWait для element_to_be_clickable(), и вы можете использовать любую из следующих стратегий локатора :

  • кодовый блок:

    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()
    
  • Примечание : необходимо добавить следующий импорт:

    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
    

Обновление

Поскольку элемент по-прежнему недоступен для нажатия с помощью WebDriverWait и Actions , в качестве альтернативы вы можете использовать метод execute_script() следующим образом:

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']"))))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...