Selenium执行javaScript
<h2>简介</h2><div>
<p> 1)selenium能够处理js,这使selenium拥有更为强大的能力,既然能够执行js,那么js能做的事情,selenium大部分也能做</p>
<p> 2)直接使用js操作页面,能解决很多click()不生效的问题</p>
<p> 3)页面滚动到底部,顶部</p>
<p> 4)处理富文本,时间控件的输入</p>
<p> </p>
</div>
<h3><span style="font-size: 16px">1. selenium调用js</span></h3>
<p>WebDriver有两个方法来执行JavaScript,分别是:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">execute_script(同步执行)
execute_async_script(异步执行)</span></pre>
</div>
<p>execute_script(script, *args): 执行js</p>
<p>return:可以返回js的返回结果</p>
<p>execute_script:arguments传参</p>
<div class="cnblogs_code">
<pre>driver.execute_script(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">return document.getElementById('kw').value</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
driver.execute_script(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">return document.title;</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 返回网页标题</span></pre>
</div>
<p> </p>
<h3>2.js 提供的定位方法</h3>
<p><strong>文档级别操作</strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">document.getElementById
document.getElementsByClassName
document.getElementsByName
document.getElementsByTagName
document.getElementsByTagNameNS</span></pre>
</div>
<p><strong>元素级别操作</strong></p>
<p>可以先使用WebDriver获取想要操作的元素,然后使用JavaScript执行操作。</p>
<div class="cnblogs_code">
<pre>input_ele = driver.find_element(By.ID, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">su</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
driver.execute_script(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">arguments.click();</span><span style="color: rgba(128, 0, 0, 1)">"</span>, input_ele)</pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> test_baidu2(self):
self.driver.get(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">http://www.baidu.com</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
input_ele </span>= self.driver.find_element_by_id(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">kw</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
self.driver.execute_script(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">arguments.value = 'test';</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, input_ele)
time.sleep(</span>2<span style="color: rgba(0, 0, 0, 1)">)
baidu_ele </span>= self.driver.find_element_by_id(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">su</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
self.driver.execute_script(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">arguments.click();</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, baidu_ele)
time.sleep(</span>2)</pre>
</div>
<p>可以在语句中使用多个 JavaScript动作</p>
<div class="cnblogs_code">
<pre>username = driver.find_element_by_xpath(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">//*[@id='username']</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
password </span>= driver.find_element_by_xpath(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">//*[@id='password']</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
driver.execute_script(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">arguments.value = 'admin';arguments.value = 'admin';</span><span style="color: rgba(128, 0, 0, 1)">"</span>, username, password)</pre>
</div>
<p> </p>
<h3><span style="font-size: 16px">3.js的滑动页面</span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">如:百度搜索后一直滑动到最底部,点击下一页
js:document.documentElement.scrollTop=10000 将页面滑动至底部,给一个非常大的值就可以
document.documentElement.scrollTop=0 给一个非常小的值就可以滑动到顶部
document.getElementById("su") 可以通过这种方式获取元素
window.scrollTo(0,document.body.clientHeight) 滑动到可视窗口的最底部
selenium:滑动到target 为可见状态:
target =self.driver.find_element_by_id("help") 或 target =driver.execute_script("return document.getElementById('help')")
self.driver.execute_script("arguments.scrollIntoView();", target)</span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> TestJS(Base):
</span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> test_js_scroll(self):
self.driver.get(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">https://www.baidu.com</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
self.driver.find_element_by_id(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">kw</span><span style="color: rgba(128, 0, 0, 1)">'</span>).send_keys(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">selenium</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> self.driver.find_element_by_id('su').click() #点击百度一下</span>
element = self.driver.execute_script(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">return document.getElementById("su")</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">点击百度一下</span>
element.click() <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 点击百度一下</span>
self.driver.execute_script(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">document.documentElement.scrollTop=5000</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 滚动到最底部</span>
time.sleep(2<span style="color: rgba(0, 0, 0, 1)">)
self.driver.find_element_by_xpath(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//*[@id="page"]/div/a/span</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">).click()
time.sleep(</span>3<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">for</span> code <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> [
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">return document.title</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">return JSON.stringify(performance.timing)</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
] :
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(self.driver.execute_script(code)) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 打印标题和内容</span></pre>
</div>
<p> </p>
<h3><span style="font-size: 16px">4.js处理时间控件</span></h3>
<p>思路:1.要取消日期的readonly属性;</p>
<p> 2.给value赋值;</p>
<p> 3.写js代码来实现如上2点,在用webdriver对js进行处理;</p>
<p> 示例:</p>
<p> <img src="https://img2022.cnblogs.com/blog/1373456/202208/1373456-20220818145908775-757217313.png" alt="" loading="lazy"></p>
<div class="cnblogs_code">
<pre class="line-numberslanguage-python"><code class="pythonlanguage-python"> <span class="token keyword">def <span class="token function">test_js_datetime<span class="token punctuation">(self<span class="token punctuation">)<span class="token punctuation">:
self<span class="token punctuation">.driver<span class="token punctuation">.get<span class="token punctuation">(<span class="token string">"https://www.12306.cn/index/"<span class="token punctuation">)
<span class="token comment"># 将出发元素赋值
time_element <span class="token operator">= <span class="token string">"document.getElementById('train_date')"
<span class="token comment"># 打印修改前的日期
<span class="token keyword">print<span class="token punctuation">(self<span class="token punctuation">.driver<span class="token punctuation">.execute_script<span class="token punctuation">(<span class="token string-interpolation"><span class="token string">f"return <span class="token interpolation"><span class="token punctuation">{time_element<span class="token punctuation">}<span class="token string">.value"<span class="token punctuation">)<span class="token punctuation">)
<span class="token comment"># 移除元素的readonly属性
self<span class="token punctuation">.driver<span class="token punctuation">.execute_script<span class="token punctuation">(<span class="token string-interpolation"><span class="token string">f"<span class="token interpolation"><span class="token punctuation">{time_element<span class="token punctuation">}<span class="token string">.removeAttribute('readonly')"<span class="token punctuation">)
<span class="token comment"># 修改元素的值
self<span class="token punctuation">.driver<span class="token punctuation">.execute_script<span class="token punctuation">(<span class="token string-interpolation"><span class="token string">f"<span class="token interpolation"><span class="token punctuation">{time_element<span class="token punctuation">}<span class="token string">.value='2020-12-30'"<span class="token punctuation">)
sleep<span class="token punctuation">(<span class="token number">2<span class="token punctuation">)
<span class="token comment"># 打印修改的日期
<span class="token keyword">print<span class="token punctuation">(self<span class="token punctuation">.driver<span class="token punctuation">.execute_script<span class="token punctuation">(<span class="token string-interpolation"><span class="token string">f"return <span class="token interpolation"><span class="token punctuation">{time_element<span class="token punctuation">}<span class="token string">.value"<span class="token punctuation">)<span class="token punctuation">)</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
</div>
<p> </p>
<h3><span style="font-size: 16px">5.获取页面标题</span></h3>
<p><span style="font-size: 16px">js: document.title </span></p>
<div class="cnblogs_code">
<pre>driver.execute_script(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">return document.title</span><span style="color: rgba(128, 0, 0, 1)">"</span>))</pre>
</div>
<p> </p>
<h3><span style="font-size: 16px">6.弹出alert弹窗</span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">js: window.alert("hello selenium”)</span>
driver.execute_script(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">window.alert('hello selenium')</span><span style="color: rgba(128, 0, 0, 1)">"</span>)</pre>
</div>
<p> </p>
<h3><span style="font-size: 16px">7. 获取当前页面的加载速度等数据 </span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">js:JSON.stringify(performance.timing)</span>
driver.execute_script(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">return JSON.stringify(performance.timing)")</span></pre>
</div>
<p> </p>
<h3><span style="font-size: 16px"> 8.其他</span></h3>
<div>
<div>
<ul>
<li><code>driver.execute_script("arguments.click();", element)</code>,点击操作</li>
<li><code>driver.execute_script("arguments.scrollIntoView();", element)</code>,移动到目标元素,类似于滑动查找</li>
</ul>
<p> </p>
<h3><span style="font-size: 16px">9.在页面的textarea文本框中输入内容</span></h3>
<p>HTML代码如下:</p>
<div class="cnblogs_code">
<pre><textarea value=<span style="color: rgba(128, 0, 0, 1)">""</span> name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">ta1</span><span style="color: rgba(128, 0, 0, 1)">"</span>></textarea></pre>
</div>
<p>虽然可以通过name定位到元素,但是不能通过send_keys()输入内容,这种情况下就可以借助javascript代码输入内容</p>
<p>代码如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">定义输入的内容</span>
input_text = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">selenium</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">将输入的内容与js拼接</span>
js = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">document.querySelector('textarea').value='</span><span style="color: rgba(128, 0, 0, 1)">"</span>+ input_text + <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">';</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(js)
driver.execute_script(js)</span></pre>
</div>
</div>
</div>
<h2>实操:通过JavaScript实现页面滚动,JavaScript操作滚动条:</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> selenium <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> webdriver
</span><span style="color: rgba(0, 0, 255, 1)">from</span> time <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> sleep
</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> casetest(object):
</span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span><span style="color: rgba(0, 0, 0, 1)">(self):
self.driver </span>=<span style="color: rgba(0, 0, 0, 1)"> webdriver.Chrome()
self.driver.get(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">http://wwww.baidu.com</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> test_execute1(self):
self.driver.execute_script(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">alert('test')</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 同步执行</span>
sleep(2<span style="color: rgba(0, 0, 0, 1)">)
self.driver.switch_to.alert.accept()
</span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> test_execute2(self):
js </span>=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">return document.title</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
title </span>= self.driver.execute_script(js)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 取到百度的title</span>
<span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(title)
</span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> test_execute3(self):
js </span>=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">var q=document.getElementById("kw");q.style.border="2px solid red"</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
self.driver.execute_script(js)</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 把百度搜索边框变为红色</span>
sleep(2<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> test_execute4(self):
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 滚动条滚动</span>
self.driver.find_element_by_id(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">kw</span><span style="color: rgba(128, 0, 0, 1)">'</span>).send_keys(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">留白</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
self.driver.find_element_by_id(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">su</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">).click()
sleep(</span>2<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 滚动到底部</span>
js = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">window.scrollTo(0,document.body.scrollHeight)</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
self.driver.execute_script(js)
sleep(</span>2<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span> == <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:
case </span>=<span style="color: rgba(0, 0, 0, 1)"> casetest()
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> case.test_execute1()</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> case.test_execute2()</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> case.test_execute3()</span>
<span style="color: rgba(0, 0, 0, 1)"> case.test_execute4()
case.driver.quit()</span></pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/yunlong-study/p/16597987.html
頁:
[1]