美文网首页
selenium对富文本框的处理

selenium对富文本框的处理

作者: 朝畫夕拾 | 来源:发表于2019-04-04 10:24 被阅读0次

一般输入框有以下几种形式

第一种:短的input框

如百度首页的输入框,<input type="text" class="s_ipt" name="wd" id="kw" maxlength="100" autocomplete="off">,百度输入框的值不在text中,是在value属性中

又验证了一下,自己写的简单的登录界面的输入框,发现确实也是这样的,html代码为

<html>

    <head>

        <meta charset="utf-8">

            <title>html表单作业-2</title>

                <script type="text/javascript">

                        function myFunc(){

                                var username = document.getElementById("username").value;

                                    confirm("用户名为:"+ username);

                             }

                         </script>

                       </head>

                    <body>

                <form>            

                    用户名:<input type="text" id="username" name="username"/>

                       <br />            

                    密码:<input type="password" id="passwd" name="passwd"/>

                    <br />

                    <input type="button" value="登录" onclick="myFunc()"/>&nbsp;&nbsp;

                       <input type="reset" value="重置"/>

                    </form>

                </body>

</html>

解决办法:driver.find_element_by_id("kw").send_keys("需要输入的内容")

第二种:div式的editor框

比如QQ邮箱写邮件,因此这种也采用的是send_keys的方法,只不过这个值不在value属性中,而是在text中

第三种:textare

以博客园的博文评论为例,发现文本值也是保存在value中的

和百度输入框不同,虽然百度输入框中的文本值也是存在value中,但它可以通过send_keys的方法来发送值,但这种必须要用到js

from selenium import webdriver 

import time 

 option = webdriver.ChromeOptions() 

option.add_argument("--user-data-dir=C:\\Users\\Beck\\AppData\\Local\\Google\\Chrome\\User Data") 

driver = webdriver.Chrome(options=option) 

driver.get("https://www.cnblogs.com/cnhkzyy/p/9253272.html") 

time.sleep(3) 

textarea_element = driver.find_element_by_id("tbCommentBody") 

driver.execute_script("arguments[0].focus();", textarea_element) 

time.sleep(2) 

driver.execute_script("arguments[0].value='test comment'", textarea_element) 

time.sleep(2) 

driver.find_element_by_id("btn_comment_submit").click() 

time.sleep(3) 

 driver.quit()

可以看到评论的确提交成功了

第四种:iframe中的editor

示例的网站是:http://ueditor.baidu.com/website/examples/completeDemo.html

我们可以看到,内容为空时,直接定位到了body,再仔细看看,原来还有个iframe

有了内容之后,<body>标签下会自动加一些<p>标签,文本内容就在<p>标签的text中

所以,对于这种情况,先切换到iframe,再向<body>标签直接send_keys,简单粗暴

from  selenium   import webdriver

import time

driver = webdriver.Chrome()

driver.get("http://ueditor.baidu.com/website/examples/completeDemo.html")

time.sleep(2)

#通过id切换到iframe

driver.switch_to.frame("ueditor_0")

content ="""This is a test content!

            This is a test content!

            This is a test content!

          """

driver.find_element_by_tag_name("body").send_keys(content)

print(driver.find_element_by_tag_name("body").text)

运行结果:

python控制台输出:

Thisis a test content!

            This is a test content!

            This isa test content!



frameset不用切,frame需层层切!

很多人在用selenium定位页面元素的时候会遇到定位不到的问题,明明元素就在那儿,用firebug也可以看到,就是定位不到,这种情况很有可能是frame在搞鬼(原因之一,改天专门说说定位不到元素,可能的一些原因及处理办法)。

frame标签有frameset、frame、iframe三种,frameset跟其他普通标签没有区别,不会影响到正常的定位,而frame与iframe对selenium定位而言是一样的,selenium有一组方法对frame进行操作。

1.怎么切到frame中(switch_to.frame())

selenium提供了switch_to.frame()方法来切换frame

switch_to.frame(reference)

不得不提到switch_to_frame(),很多人在这样写的时候会发现,这句话被划上了删除线,原因是这个方法已经out了,之后很有可能会不支持,建议的写法是switch_to.frame()

reference是传入的参数,用来定位frame,可以传入id、name、index以及selenium的WebElement对象,假设有如下HTML代码 index.html:

<html lang="en">

<head>

    <title>FrameTest</title>

</head>

<body>

<iframe src="a.html" id="frame1" name="myframe"></iframe>

</body>

</html>

想要定位其中的iframe并切进去,可以通过如下代码:

from selenium import webdriver

driver = webdriver.Firefox()

driver.switch_to.frame(0)  # 1.用frame的index来定位,第一个是0

# driver.switch_to.frame("frame1")  # 2.用id来定位

# driver.switch_to.frame("myframe")  # 3.用name来定位

# driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))  # 4.用WebElement对象来定位

通常采用id和name就能够解决绝大多数问题。但有时候frame并无这两项属性,则可以用index和WebElement来定位:

index从0开始,传入整型参数即判定为用index定位,传入str参数则判定为用id/name定位

WebElement对象,即用find_element系列方法所取得的对象,我们可以用tag_name、xpath等来定位frame对象

举个栗子:

<iframe src="myframetest.html" />

用xpath定位,传入WebElement对象:

driver.switch_to.frame(driver.find_element_by_xpath("//iframe[contains(@src,'myframe')]"))

2.从frame中切回主文档(switch_to.default_content())

切到frame中之后,我们便不能继续操作主文档的元素,这时如果想操作主文档内容,则需切回主文档。

driver.switch_to.default_content()

3.嵌套frame的操作(switch_to.parent_frame())

有时候我们会遇到嵌套的frame,如下:

<html>

    <iframe id="frame1">

        <iframe id="frame2" / >

    </iframe>

</html>

1.从主文档切到frame2,一层层切进去

driver.switch_to.frame("frame1")

driver.switch_to.frame("frame2")

2.从frame2再切回frame1,这里selenium给我们提供了一个方法能够从子frame切回到父frame,而不用我们切回主文档再切进来。

driver.switch_to.parent_frame()  # 如果当前已是主文档,则无效果

有了parent_frame()这个相当于后退的方法,我们可以随意切换不同的frame,随意的跳来跳去了。

所以只要善用以下三个方法,遇到frame分分钟搞定:

driver.switch_to.frame(reference)

driver.switch_to.parent_frame()

driver.switch_to.default_content()

补充

另外补充一下,之前曾看到过用点分法来切入嵌套frame的方法,但我试验之后发现并不能定位到frame,如果有同学可以成功,麻烦留言告知一下,用法如下:

driver.switch_to.frame('frame1.0.frame3')

据说以上代码可以切到 “frame1” 下的 “第一个frame” 下的 “frame3” 中。

相关文章

网友评论

      本文标题:selenium对富文本框的处理

      本文链接:https://www.haomeiwen.com/subject/dztfiqtx.html