精華 selenium_webdriver(python)調用js腳本


#coding=utf-8
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get("https://www.baidu.com/")
#給搜索輸入框標紅javascript腳本
js="var q=document.getElementById(\"kw\");q.style.border=\"2px solid red\";"
#調用給搜索輸入框標紅js腳本
driver.execute_script(js)
time.sleep(3)
#單獨執行js腳本
driver.execute_script('alert("輸入框標紅了!")')
time.sleep(3)
#接受提示信息 
driver.switch_to_alert().accept()
time.sleep(3)
#js隱藏元素,將獲取的圖片元素隱藏
img1 = driver.find_element_by_xpath("//*[@id='lg']/img")
driver.execute_script('$(arguments[0]).fadeOut()',img1)
time.sleep(3)
driver.find_element_by_xpath("//*[@id='kw']").send_keys("selenium")
driver.find_element_by_xpath("//*[@id='su']").click()
time.sleep(3)
driver.quit()

原文地址:https://blog.csdn.net/u013372487/article/details/46502853

 

js中設置元素class的三種方法小結

<!DOCTYPE HTML> 
<HTML> 
<HEAD> 
<meta charset="utf-8" /> 
<title>el.className = 'abc'</title> 
<style type="text/css"> 
.abc { 
background: red; 
} 
</style> 
</HEAD> 
<BODY> 
<div id="d1">test div</div> 
<script> 
var div = document.getElementById('d1'); 
div.className = 'abc'; 
</script> 
</BODY> 
</HTML>

 

js="var div = document.getElementById(\"kw\");div.className = \"abc\";"
driver[0].execute_script(js)
先定位到ID 然后修改這個ID的class屬性
//JavaScript屬性選擇器 選中后用來更改屬性值
var a=document.querySelector("div[data-hover-cls=inputs-hover]");
//更改屬性值class為“”
a.setAttribute("class","")
//注意此方法只適用於ID CLASS TAG之外的子屬性的選擇

 

js = "var a=document.querySelector(\"div[data-hover-cls=inputs-hover]\");a.setAttribute(\"class\",\"inputs J_LaterHover inputs-hover\")"
        self.driver.execute_script(js)

        jiage_mini = "var a=document.querySelector(\"input[aria-label=價格最小值]\");a.setAttribute(\"value\",\"50\")"
        self.driver.execute_script(jiage_mini)
        jiage_max = "var a=document.querySelector(\"input[aria-label=價格最大值]\");a.setAttribute(\"value\",\"50\")"
        self.driver.execute_script(jiage_max)

        submit="var a=document.querySelector(\"button[type=button]\"),a.click()"
        self.driver.execute_script(submit)
小例子

TB翻頁

 var button=document.querySelector("a[trace=srp_select_pagedown]")

 

XP查看更多

var button=document.querySelector("div[class=item-collection-detail__products-more]")
button.click()


 

蝦皮

單次點擊【查看更多】
var button=document.querySelector("div[class=item-collection-detail__products-more]")
button.click()

循環點擊【查看更多】
for (var i=1; i<10; i++)
{

//延時1秒執行 https://www.oschina.net/question/141209_21625?sort=time
var beginTime=new Date().getTime();
while(new Date().getTime() < beginTime + 2000) {

}
//要執行的代碼
var button=document.querySelector("div[class=item-collection-detail__products-more]");
button.click();
}

循環點擊新增

//class選擇器【分類添加商品】
var a=document.getElementsByClassName("item-collection-detail__products-item__action")
for (var i=1; i<10; i++)
{ a[i].click();
//延時器
var beginTime=new Date().getTime();
while(new Date().getTime() < beginTime + 200) {
}
}
//i=商品數量 beginTime=間隔時間


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM