一尘不染

如何在Chrome中单击通过WebDriver通过Ajax加载的元素

selenium

浏览器:Chrome V65

ChromeDriver:chromedriver.exe 2.37

网络驱动程序尝试单击元素时发生错误。以下是我的click():

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

这种情况在Chrome上经常发生,可能是5次出现4次故障。没用!

现在,我必须使用sleep来等待元素被点击。

有人可以帮忙吗?谢谢。


阅读 325

收藏
2020-06-26

共1个答案

一尘不染

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

句法:

Actions action = new Actions(driver);
action.moveToElement("Your Element").click().perform();
2020-06-26