您的位置:首頁技術文章
文章詳情頁

Selenium Web驅動程序和Java元素在(x,y)點處不可單擊其他元素將獲得點擊?

瀏覽:107日期:2024-05-06 17:06:30
(adsbygoogle = window.adsbygoogle || []).push({}); 如何解決Selenium Web驅動程序和Java元素在(x,y)點處不可單擊其他元素將獲得點擊??

你可以通過以下任一過程解決它們:

1.由于存在JavaScript或AJAX調用而無法單擊元素

嘗試使用ActionsClass:

WebElement element = driver.findElement(By.id('navigationPageButton'));Actions actions = new Actions(driver);actions.movetoElement(element).click().build().perform();2.由于元素不在視口中,因此無法單擊

嘗試用于JavascriptExecutor將元素帶入視口中:

WebElement myelement = driver.findElement(By.id('navigationPageButton'));JavascriptExecutor jse2 = (JavascriptExecutor)driver;jse2.executeScript('arguments[0].scrollIntoView()', myelement);3.在元素可單擊之前,頁面正在刷新。

在這種情況下,請誘導ExplicitWait,即第4點中提到的webdriverwait。

4.元素存在于DOM中,但不可單擊。

在這種情況下, 將ExplicitWaitExpectedConditions設置為,elementToBeClickable以使元素可單擊:

webdriverwait wait2 = new webdriverwait(driver, 10);wait2.until(ExpectedConditions.elementToBeClickable(By.id('navigationPageButton')));5.元素存在但具有臨時覆蓋。

在這種情況下,ExplicitWait使用 ExpectedConditions設置invisibilityOfElementLocated為可使Overlay不可見。

webdriverwait wait3 = new webdriverwait(driver, 10);wait3.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath('ele_to_inv')));6.元素存在但具有永久覆蓋。

用于JavascriptExecutor直接在元素上發送點擊。

WebElement ele = driver.findElement(By.xpath('element_xpath'));JavascriptExecutor executor = (JavascriptExecutor)driver;executor.executeScript('arguments[0].click();', ele);解決方法 Selenium 單擊

我使用了明確的等待,并發出警告:

org.openqa.selenium.WebDriverException:元素在點(36,72)不可單擊。其他元素將獲得點擊:…命令持續時間或超時:393毫秒

如果我使用Thread.sleep(2000)我不會收到任何警告。

@Test(dataProvider = "menuData")public void Main(String btnMenu,String TitleResultPage,String Text) throws InterruptedException { WebDriverWait wait = new WebDriverWait(driver,10); driver.findElement(By.id("navigationPageButton")).click(); try { wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(btnMenu))); } catch (Exception e) {System.out.println("Oh"); } driver.findElement(By.cssSelector(btnMenu)).click(); Assert.assertEquals(driver.findElement(By.cssSelector(TitleResultPage)).getText(),Text);}

標簽: java
国产综合久久一区二区三区