我正在嘗試創建一個預訂活動的測驗腳本......但我正在嘗試選擇并單擊此頁面上的“立即預訂”按鈕...... https://www.trybooking.com/events/landing?eid=757231&
單擊“立即預訂”按鈕應將預訂者帶到會話頁面.. https://www.trybooking.com/book/sessions?eid=757231
相反,我的代碼會導致 Junit/Java 例外...
我可能會嘗試選擇按鈕并單擊它,以便它轉到會話頁面.. 使用以下... [這些已在代碼中注釋掉]...
String url = "https://www.trybooking.com/events/landing?eid=757231&";
myDriver.get(url);
//WebElement we = myDriver.findElement(By.xpath("//span/button[text()='Book now' and @class='btn btn-secondary text-uppercase btn-landing go-to-session']"));
//WebElement we = myDriver.findElement(By.xpath("//span/button[text()='Book now']"));
// WebElement we = myDriver.findElement(By.xpath("//span/button[text()='BOOK NOW']"));
//WebElement wb = myDriver.findElement(By.xpath ("//*[contains(text(),'Book now')]"));
//WebElement wb = new WebDriverWait(myDriver, Duration.ofSeconds(50, 1)).
//WebElement wb = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(),'Book now')]")));
//WebElement wb = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[contains(text(),'Book now') ]")));
//WebElement wb = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='btn btn-secondary text-uppercase btn-landing go-to-session']" )));
//wb.click();
//wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='btn btn-secondary text-uppercase btn-landing go-to-session']" ))).click();
//WebElement wb = myDriver.findElement(By.xpath ("//button[@class='btn btn-secondary text-uppercase btn-landing go-to-session']"));
//WebElement wb = myDriver.findElement(By.xpath ("//div[@class='pl-0 mr-3 sticky-btn-wrapper'] and button[@class='btn btn-secondary text-uppercase btn-landing go-to-session']"));
//WebElement BookNowButton = myDriver.findElement(By.xpath ("//div[@class='pl-0 mr-3 sticky-btn-wrapper']//button"));
我最近的嘗試和硒代碼......我選擇這個的硒代碼是......
// added at the start of the Junit ... JavascriptExecutor js = (JavascriptExecutor) myDriver;
@DisplayName("TC03: Book into an event (ADVANCED TASK)")
void Test3()
// commented out.. throws Exception
{
//myDriver.get("https://www.trybooking.com/book/search");
WebDriverWait wait = new WebDriverWait(myDriver, Duration.ofSeconds(10, 1));
myDriver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
String url = "https://www.trybooking.com/book/search";
myDriver.get(url);
myDriver.findElement(By.id("initSearchKeywordId")).click();
myDriver.findElement(By.id("initSearchKeywordId")).sendKeys("Makerspace Docklands - Safety Induction");
myDriver.findElement(By.id("initSearchButtonId")).click();
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("pre-load-spinner") ));
WebElement we = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='search-heading' and starts-with(@id, 'newSearchNavigateHeadingEventId')]/h2[contains(., 'Makerspace Docklands - Safety Induction') and contains(@ng-bind, 'EventDisplayName')]")));
we.click();
// now to click on the book Now button on the "Makerspace Docklands - Safety Induction" page
WebElement BookNowButton = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath( "//div[@class='pl-0 mr-3 sticky-btn-wrapper']//button" )));
//This will scroll the page till the element is found
js.executeScript("arguments[0].scrollIntoView();",BookNowButton);
BookNowButton.click();
// more code to follow
}// close void Test3()
我收到以下錯誤訊息...
java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.JavascriptExecutor.executeScript(String, Object[])" because "this.js" is null
有什么建議?
uj5u.com熱心網友回復:
當驅動程式實體沒有與每個類中的 selenium 互動時,會發生空點例外
解決方案——在全域變數中定義webdriver驅動
WebDriver 驅動程式;
uj5u.com熱心網友回復:
認為這可能是問題,您的宣告尚未構建,因此“this.js”為空:
JavascriptExecutor js = (JavascriptExecutor) myDriver;
js.executeScript("arguments[0].scrollIntoView();",BookNowButton);
正如您通常必須將 JavascriptExecutor 的使用定義/構造為:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();",BookNowButton);
嘗試以下操作以避免“this.js”為空,假設在您的代碼中它應該是:
JavascriptExecutor js = ((JavascriptExecutor) myDriver);
js.executeScript("arguments[0].scrollIntoView();",BookNowButton);
如果問題仍然存在,請嘗試使用以下方法進行故障排除:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();",BookNowButton);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/343801.html
