如何单击通过Chrome内Web Driver通过AJAX加载的元素



浏览器:Chrome V65

chromedriver:chromedriver.exe 2.37

试图单击元素的WebDriver时发生错误。以下是我的点击((:

def click(self):
    try:
        self.wait_for().visible()
        self._selenium_context().click()
    except Exception as e:
        raise NoSuchElementException
def visible(self):
    '''
    Check if the element is visible.
    :return:  True or exception.
    '''
    return Utils.wait_for(self.web_element.visible, self.interval, self.timeout)

我已经等待可见元素,然后单击。但是,抛出了例外,说"其他元素会收到点击"如下:

selenium.common.exceptions.WebDriverException: Message: unknown error: Element <div class="learn-wrap" ng-click="changeTab(2)" ng-class="internal.tab == 2?'learn-selected':''">...</div> is not clickable at point (1026, 89). Other element would receive the click: <div class="loading-data ng-scope ng-animate ng-leave ng-leave-active" ng-if="internal.isAjaxing" data-ng-animate="2" style="">...</div>

错误也会发生错误,即使我添加语句等待ajax加载完成以单击元素:

driver.find_element(By.XPATH, "//div[contains(@class, 'learn') and (contains(@ng-if, '!internal.isAjaxing'))]")
driver.find_element(By.XPATH , element_xpath).click()

这在铬中经常发生,大概在失败的5倍中发生了4个。它行不通!

现在,我必须使用睡眠才能等待元素可单击。

有人可以帮忙吗?谢谢。

您可以使用动作类单击元素,

语法:

Actions action = new Actions(driver);
action.moveToElement("Your Element").click().perform();

如您所提到的 ajax调用涉及,您正在使用睡眠来等待元素可单击,而不是您需要使用 WebDriverWait 以单击以下单击:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC    
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class, 'learn') and (contains(@ng-if, '!internal.isAjaxing'))]"))).click()

相关内容

  • 没有找到相关文章

最新更新