单击按钮,硒中无响应

时间:2020-01-14 06:26:56

标签: selenium selenium-webdriver

我必须首先选择Unsubscription,然后单击Continueenter image description here

我试图通过Selectorxpath查找并执行原始的javescript。但是,单击按钮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()

这是负责Continue按钮的HTML代码。 enter image description here

点击“取消订阅”后,这是Unsubscriptioncontinue按钮:

<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 :(得分:0)

可能甚至在元素正确加载之前即JavaScript / AJAX调用完成之前,您正在调用click()

您需要为element_to_be_clickable()引入 WebDriverWait ,并且可以使用以下Locator Strategies中的任何一个:

  • 代码块:

    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']"))))

答案 1 :(得分: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();
            }

将定位符作为这样的参数传递,并等待该元素可单击。 在单击之前调用此方法,以便它将等待直到元素可单击并执行单击操作。