Java+Selenium調(diào)用JavaScript的方法詳解
簡(jiǎn)介
本文主要講解java 利用Selenium 操作瀏覽器網(wǎng)站時(shí)候,需要用的js的地方,代碼該如何實(shí)現(xiàn)。
調(diào)用JavaScript
webdriver 對(duì)于滾動(dòng)條的處理需要用到 JavaScript ,同時(shí)也可以向 textarea 文本框中輸入文本( webdriver 只能定位,不能輸入文本),webdriver 中使用execute_script方法實(shí)現(xiàn) JavaScript 的執(zhí)行。
滑動(dòng)滾動(dòng)條
通過 x ,y 坐標(biāo)滑動(dòng)
對(duì)于這種通過坐標(biāo)滑動(dòng)的方法,我們需要知道做表的起始位置在頁(yè)面左上角(0,0),下面看一下示例,滑動(dòng) CSDN 首頁(yè)。
import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import java.io.IOException; public class SeleniumDemo { private final static String webDriver = "webdriver.chrome.driver"; private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe"; public static void main(String[] args) throws InterruptedException, IOException { System.setProperty(webDriver, webDriverPath); WebDriver driver= new ChromeDriver(); driver.get("https://blog.csdn.net/"); Thread.sleep(2000); JavascriptExecutor jse= (JavascriptExecutor)driver; //滑動(dòng)到距離頂部500px的位置 jse.executeScript("window.scrollTo(0,500);"); } }
通過參照標(biāo)簽滑動(dòng)
import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import java.io.IOException; public class SeleniumDemo { private final static String webDriver = "webdriver.chrome.driver"; private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe"; public static void main(String[] args) throws InterruptedException, IOException { System.setProperty(webDriver, webDriverPath); WebDriver driver= new ChromeDriver(); driver.get("https://blog.csdn.net/"); Thread.sleep(2000); JavascriptExecutor jse= (JavascriptExecutor)driver; //i要從1開始,否則 div["+i+"] 要寫成div["+(i+1)+"] for (int i = 1; i < 20; i++) { Thread.sleep(1000); //定位文章的元素 WebElement element=driver.findElement(By.xpath("http://div[@class='Community']/div["+i+"]")); //滑動(dòng)到指定元素位置 jse.executeScript("arguments[0].scrollIntoView();", element); } } }
通過循環(huán)的方式,從第一個(gè)文章的位置,依次滑倒到第20個(gè)文章的位置。
按鈕點(diǎn)擊
有些網(wǎng)站設(shè)置的反扒機(jī)制,通過 element.click()的方法,有時(shí)候?yàn)g覽器頁(yè)面的元素沒有加載完之前,會(huì)報(bào)錯(cuò),必須元素不能點(diǎn)擊的錯(cuò)誤,雖然selenium可以設(shè)置元素等待,等元素加載完畢,再點(diǎn)擊,但是有些點(diǎn)擊是由網(wǎng)頁(yè)中加載的js控制的點(diǎn)擊事件。也就是說,你的點(diǎn)擊按鈕,html\css執(zhí)行完了,在頁(yè)面上渲染出來一個(gè)點(diǎn)擊按鈕,但是 有一些js代碼還么加載完畢,如果你此時(shí)去點(diǎn)擊按鈕,就會(huì)報(bào)錯(cuò)或者不執(zhí)行,主要有兩種情況,一是,按鈕的點(diǎn)擊事件,在這個(gè)js里寫著,js沒加載完你點(diǎn)擊的話會(huì)沒有反應(yīng)。
二是,有些js會(huì)事件會(huì)會(huì)向網(wǎng)頁(yè)插入html的元素代碼,導(dǎo)致你定位好按鈕元素位置發(fā)生的變化(把按鈕元素?cái)D到別的位置),你去點(diǎn)擊的時(shí)候,執(zhí)行點(diǎn)擊的位置,已經(jīng)不是按鈕位置了,點(diǎn)擊到了別的元素報(bào)錯(cuò)了。
//指定元素位置執(zhí)行js點(diǎn)擊事件 jse.executeScript("arguments[0].click();", element);
打開新窗口
//新標(biāo)簽頁(yè)打開 String js = "window.open('https://tarzan.blog.csdn.net/')"; JavascriptExecutor jse= (JavascriptExecutor)driver; jse.executeScript(js);
到此這篇關(guān)于Java+Selenium調(diào)用JavaScript的方法詳解的文章就介紹到這了,更多相關(guān)Java Selenium調(diào)用JavaScript內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Validation和Hibernate Validator結(jié)合國(guó)際化代碼實(shí)例
這篇文章主要介紹了Spring Validation和Hibernate Validator結(jié)合國(guó)際化代碼實(shí)例,我們需要對(duì)請(qǐng)求參數(shù)進(jìn)行非空、長(zhǎng)度、正確性進(jìn)行校驗(yàn), 本文主要講解Spring Validation 和 Hibernate Validator, 同時(shí)整合i18n(國(guó)際化)實(shí)現(xiàn)參數(shù)校驗(yàn)自動(dòng),需要的朋友可以參考下2023-10-10springboot-curd基于mybatis項(xiàng)目搭建
這篇文章主要介紹了springboot-curd基于mybatis項(xiàng)目搭建,圍繞相關(guān)資料展開詳細(xì)內(nèi)容,希望對(duì)正在學(xué)習(xí)的你有所幫助,需要的小伙伴也可以參考一下2022-01-01Java Socket編程(三) 服務(wù)器Sockets
Java Socket編程(三) 服務(wù)器Sockets...2006-12-12Java中this和super的區(qū)別及this能否調(diào)用到父類使用
這篇文章主要介紹了Java中this和super的區(qū)別及this能否調(diào)用到父類使用,this和super都是Java中常見的關(guān)鍵字,下文關(guān)于兩者區(qū)別介紹,需要的小伙伴可以參考一下2022-05-05Java實(shí)現(xiàn)動(dòng)態(tài)規(guī)劃背包問題
本文主要介紹使用java實(shí)現(xiàn)動(dòng)態(tài)規(guī)劃的背包問題,詳細(xì)使用圖文和多種案例進(jìn)行解析,幫助理解該算法2021-06-06上傳自己的jar包到maven中央倉(cāng)庫(kù)的快速操作方法
網(wǎng)絡(luò)上可以搜索到很多jar包到中央倉(cāng)庫(kù),但是都不是多適合自己的項(xiàng)目,于是自己動(dòng)手寫個(gè),本文檔通過sonatype上傳jar包至maven中央倉(cāng)庫(kù),Sonatype通過JIRA來管理OSSRH倉(cāng)庫(kù),具體實(shí)例代碼跟隨小編一起看看吧2021-08-08