#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的三种方法小结
"utf-8" />el.className = 'abc' "d1">test div
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=间隔时间