我正在嘗試在魁北克政府網站上搜索法律名稱及其相關的 PDF,但是當我嘗試打開所有不同法律的選項卡以獲取其 PDF 鏈接時,當它嘗試打開第 9 個鏈接時出現 ElementNotInteractable 例外。我嘗試自行打開鏈接,它可以正常打開,但是當它通過所有法律時,它會停在那里并給我那個例外。這是我的代碼片段:
static SortedMap<String,String> QuebecConsolidatedStatutesAndPDFs = new TreeMap<String,String>();
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\WorkSpace\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(5000));
driver.get("http://www.legisquebec.gouv.qc.ca/en/chapters?corpus=statutes&selection=all");
Thread.sleep(5000);
List<WebElement> QuebecConsolidatedStatutes = driver.findElements(By.xpath("//body/div/div/div[2]/div/div[2]/table/tbody/tr[contains(@class, 'clickable')]/td/a"));
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
String parent = driver.getWindowHandle();
for (int i=0; i<QuebecConsolidatedStatutes.size(); i ){
String opentabs = Keys.chord(Keys.CONTROL, Keys.ENTER);
wait.until(ExpectedConditions.visibilityOf(QuebecConsolidatedStatutes.get(i)));
QuebecConsolidatedStatutes.get(i).sendKeys(opentabs);
}
uj5u.com熱心網友回復:
這里有幾個問題:
- 主要問題是您必須將要單擊的元素滾動到視圖中。您的默認初始螢屏高度顯示 8 行,而要單擊第 9 行,您必須先將該元素滾動到視圖中。
- 您可以將驅動程式視窗設定為更好的尺寸,這將顯示更多螢屏,但是您仍然需要滾動,但在 15 個元素之后。
- 你應該改進你的定位器。
- 你不應該混淆
WebDriverWait和implicitlyWait。
這應該會更好:
static SortedMap<String,String> QuebecConsolidatedStatutesAndPDFs = new TreeMap<String,String>();
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\WorkSpace\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(5000));
driver.manage().window().maximize();
driver.get("http://www.legisquebec.gouv.qc.ca/en/chapters?corpus=statutes&selection=all");
wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.cssSelector("tr.clickable a"), 100));
Thread.sleep(50);
List<WebElement> QuebecConsolidatedStatutes = driver.findElements(By.cssSelector("tr.clickable a"));
String parent = driver.getWindowHandle();
for (int i=0; i<QuebecConsolidatedStatutes.size(); i ){
String opentabs = Keys.chord(Keys.CONTROL, Keys.ENTER);
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", QuebecConsolidatedStatutes.get(i));
wait(300);
wait.until(ExpectedConditions.visibilityOf(QuebecConsolidatedStatutes.get(i)));
QuebecConsolidatedStatutes.get(i).sendKeys(opentabs);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/415049.html
標籤:
