Selenium是一個眾所周知的開源、基於Web的自動化工具,被許多人使用。但是有時它在與某些元素進行交互時會遇到問題;也許一個意外的彈出視窗會阻礙Web驅動程式執行操作並產生錯誤的結果。這就是JavascriptExecutor在這種情況下發揮關鍵作用的地方,它使Web驅動程式能夠成功執行所需的操作。它的複雜性和突發性並存,使得處理這種情況變得更加容易。
使用名為JavascriptExecutor的接口,可以透過Selenium執行JavaScript,並在使用這種程式語言時與瀏覽器中的HTML進行交互,必須使用JavascriptExecutor對象,創建長度和復雜度各異的句子結構對於構成引人入勝的文字至關重要。因此,JavaScript Executor提供了與網頁瀏覽器內的HTML進行溝通的手段,同時也使程式設計師能夠使用自己獨特的JavaScript編寫風格來建立巧妙靈活的表達。
以下是Selenium中JavascriptExecutor提供的方法:
#Executing JavaScript in the presently chosen window or frame has never been so easy! By calling an anonymous function, this method enables users to reap the rewards of a multitude of data types, including −
學習如何使用JavascriptExecutor
- 導入套件
#import org.openqa.selenium.JavascriptExecutor;
- 建立一個引用
#javascriptExecutor js = (JavascriptExecutor) driver;
- 呼叫JavascriptExecutor方法
js.executeScript(script, args);
// importing the package Import org.openqa.selenium.JavascriptExecutor; // creating a reference JavascriptExecutor js = (JavascriptExecutor) driver; // calling the method js.executeScript(script, args);
JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("location.reload()");
Example 2
JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("document.getElementByID(‘element id ’).value = ‘xyz’;");
Example 3
JavascriptExecutor js = (JavascriptExecutor)driver; Js.executeScript("alert(‘hello world’);");
Example 4
avascriptExecutor js = (JavascriptExecutor)driver; string sText = js.executeScript("return document.documentElement.innerText;").toString();
Example 5
avascriptExecutor js = (JavascriptExecutor)driver; string sText = js.executeScript("return document.title;").toString();
Example 6
的中文翻譯為:JavascriptExecutor js = (JavascriptExecutor)driver; //Vertical scroll – down by 150 pixels js.executeScript("window.scrollBy(0,150)");
使用javascriptExecutor選擇一個元素
Explanation
import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; public class waytoclass { public static void main(String args[]) { System.setProperty( "webdriver.edge.driver", "C:\Users\ADMIN\Documents\Selenium\msedgedriver.exe"); // Instantiate a Driver class. WebDriver driver = new EdgeDriver(); // Maximize the browser driver.manage().window().maximize(); // Launch Website driver.get("https://www.waytoclass.com/"); WebElement java = driver.findElement( By.xpath("//*[@id="hslider"]/li[6]/a")); // Create a reference JavascriptExecutor js = (JavascriptExecutor)driver; // Call the JavascriptExecutor methods js.executeScript("arguments[0].click();", java); } }
Starting MSEdgeDriver 98.0.1108.56 (9a336a18ae89157b3c7ea0568a9cbced8ebc3f7) on port 55401 Only local connections are allowed. Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping MSEdgeDriver safe. MSEdgeDriver was started successfully.注意
- 在顯示上述輸出後,它將自動開啟網站並點擊元素。
結論
以上是Selenium中的JavascriptExecutor的詳細內容。更多資訊請關注PHP中文網其他相關文章!