日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

自动化操作——JS

發布時間:2024/9/20 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自动化操作——JS 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、概念

  webdriver通過執行javascript語句,操作頁面。webdriver執行js有兩種方式同步與異步,execute_script(),execute_async_script(),前者影響后續的代碼順序,即必須等js執行完成才可以繼續,后者反之。

  在selenium自動化中調用js方式,注:也可以使用JQ語法進行操作:

driver.execute_script("js_code") #用于直接操作result=driver.execute_script("return js_code") #獲取js返回值driver.execute_script("arguments[0].scrollIntoView(true);", element); #入參兩個參數,前者是js代碼,后者是元素對象

二、語法

  1、定位

    document.getElementById //根據ID查找元素,大小寫敏感,如果有多個結果,只返回第一個;document.getElementsByClassName //根據類名查找元素,多個類名用空格分隔,返回一個 HTMLCollection //另外,不僅僅是document,其它元素也支持getElementsByClassName 方法;document.getElementsByTagName //根據標簽查找元素, * 表示查詢所有標簽,返回一個 HTMLCollection 。document.getElementsByName //根據元素的name屬性查找,返回一個 NodeList 。document.querySelector //返回單個Node,IE8+(含),如果匹配到多個結果,只返回第一個。document.querySelectorAll //返回一個 NodeList ,IE8+(含)。document.forms //獲取當前頁面所有form,返回一個 HTMLCollection ;

  2、獲取界面信息

    document.body.scrollWidth; //獲取完整網頁正文全文寬,包括有滾動條時的未見區域

    document.body.scrollHeight; //獲取完整網頁正文全文高,包括有滾動條時的未見區域  
   

    document.documentElement.clientWidth; //僅獲取可見區域寬度,不有滾動條時的未見區域

    document.documentElement.clientHeight; //僅獲取可見區域高度,不有滾動條時的未見區域
?

    document.documentElement.scrollTop=100; //設置或返回匹配元素相對滾動條頂部的偏移,即獲取網頁被卷去的高度

    document.documentElement.scrollLeft=100 ; //設置或返回匹配元素相對滾動條左側的偏移 ,即獲取網頁被卷去的左部分


    document.documentElement.offsetTop; //獲取對象相對于由offsetParent屬性指定的父坐標(css定位的元素或body元素)距離頂端的????????????????????????????????????????????? ?
?????????????????????????????????????????? //高度

    document.documentElement.offsetLeft; //獲取對象相對于由offsetParent屬性指定的父坐標(css定位的元素或body元素)的高度IE、
?????????????????????????????????????????? //Opera 認為offsetHeight = clientHeight + 滾動條 + 邊框。FF 認為offsetHeight
?????????????????????????????????????????? //是網頁內容實際高度,可以小于 clientHeight。offsetHeight在新版本的FF和IE中是一樣的???????????????????????????????? ?
?????????????????????????????????????????? //表示網頁的高度,與滾動條無關,chrome中不包括滾動條。
                         //詳見 https://blog.csdn.net/w390058785/article/details/80461845

    window.screen.height;? //屏幕分辨率的高
    window.screen.width;?? //屏幕分辨率的寬
    
    document.documentElement.style;? //獲取行內式標簽style屬性,html 標簽上直接寫
??????????????????????????????????????? //<input style='color:red;' > 這樣才可以取到
    document.documentElement.getComputedStyle()? //獲取對象css屬性
    //詳見 https://www.cnblogs.com/xiyangbaixue/p/4001531.html

  3、操作界面元素

    window.scrollTo(100,400);   //滾動位置left=100,top=400document.getElementsByClassName("name")[0].scrollIntoViewIfNeeded(true) //滾動到name元素顯示document.getElementById(“id”).value="你想設置的文字" //給元素設置文本值document.getElementsByClassName("name")[0].scrollIntoViewIfNeeded(true) //滾動到name頁面居中顯示document.getElementsByClassName("comment-content")[0].scrollIntoView()  //滾動到name頁面頂上顯示document.getElementById("query").style.display="none" // 隱藏document.getElementById("query").style.display="block" // 可見document.getElementsByClassName("sec-input")[0].disabled=false // 取消置灰document.getElementById("query").removeAttribute('readonly') // 移除'readonly'屬性,是元素可輸入document.getElementById("query").innerHTML // 獲取HTML內容document.getElementsByClassName('top-nav')[0].innerText // 獲取文本內容document.getElementsByClassName("sec-input")[0].attributes.屬性 //獲取元素屬性//滾動條拖動到元素位置element=driver.find_element_by_id('auto')driver.execute_script('arguments[0].scrollIntoViewIfNeeded(true);',element)

?

總結

以上是生活随笔為你收集整理的自动化操作——JS的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。