Selenium+Python+Unittest:执行测试“其他元素将收到单击”时出现错误消息

2024-05-15 12:49:49 发布

您现在位置:Python中文网/ 问答频道 /正文

详细信息: 在我当前的项目中,我正在Magento中编写关于Python和Selenium的测试用例。在此过程中,我需要一个滑动面(按钮)

请求:

def test_pagebuilder_slide_button_pagebuilder_button_primary(self):
        driver = self.driver
        driver.get("my_webseite.de")
        time.sleep(15)
        try: driver.find_element_by_id("slick-slide-control01").click()
        except AssertionError as e: self.verificationErrors.append(str(e)) 
        time.sleep(15)

现在我得到了这样一个信息:测试是有效的,因为另一个方面会阻止这种情况。这是我在身份证搜索后直接做的,这是可用的。作为急救,我也休息了一下,也许这就是为什么

错误消息:

selenium.common.exceptions.WebDriverException: Message: unknown error: Element <button type="button" role="tab" id="slick-slide-control01" aria-controls="slick-slide01" aria-label="... of 2" tabindex="-1">2</button> is not clickable at point (517, 612). Other element would
receive the click: <div role="alertdialog" tabindex="-1" class="message global cookie" id="notice-cookie-block" style="">...</div>

你知道我怎样才能避免这种情况吗


Tags: selfidtimedriver情况buttonsleepelement
3条回答

通过使用javascript执行单击来绕过此问题:

browser.execute_script(document.getElementById("slick-slide-control01").click())

还值得考虑其他可能在主页上注入元素的脚本。鉴于错误的性质,这看起来很像GDPR合规性插件中的一个,它会强制用户发出警报。最好的选择是像阿尤斯提到的那样放弃它,然后继续你的剧本

此错误的一个常见实例是当元素在页面上不可见时,例如,您可能需要滚动以访问该元素。 如果是这样的话ActionChains就应该这样做:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "slick-slide-control01")))
ActionChains(driver).move_to_element(driver.find_element_by_id("slick-slide-control01")).click().perform()

相关问题 更多 >