我正在嘗試單擊日歷,但每次嘗試單擊日歷時,都會彈出錯誤“元素單擊被攔截:元素在點 (293, 1317) 不可單擊
<input type="text" name="form_fields[travel_comp_date]" id="form-field-travel_comp_date"
class="elementor-field elementor-size-sm elementor-field-textual elementor-date-field
flatpickr-input" placeholder="Date of travel"
pattern="[0-9]{4}-[0-9]{2}-[0-9]{2}" readonly="readonly">
這是我的代碼:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='form_fields[travel_comp_date]']")));
driver.findElement(By.xpath("//input[@name='form_fields[travel_comp_date]']")).click();
有人可以幫我糾正這個問題。
uj5u.com熱心網友回復:
您嘗試訪問的元素超出了最初呈現的視口。
為了單擊它,您首先需要滾動頁面以將該元素帶入可見視圖埠,然后才能單擊它。
請試試這個:
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");
或這個
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,600)");
之后嘗試執行您的代碼
uj5u.com熱心網友回復:
請在顯式等待的情況下嘗試以下代碼,并讓我知道它是否適合您:
enter code here
driver.get("https://www.path2usa.com/travel-companion/");
WebElement dateTravel =
driver.findElement(By.xpath("//input[@id='form-field-
travel_comp_date']"));
driver.manage().window().maximize();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,1000)");
WebDriverWait wait = new WebDriverWait(driver,
Duration.ofSeconds(5));
js.executeScript("arguments[0].scrollIntoView();", dateTravel);
wait.until(ExpectedConditions.visibilityOf(dateTravel));
if(dateTravel.isDisplayed())
{
js.executeScript("document.getElementById('form-field-
travel_comp_date').value='12/20/2023'");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/506706.html
