使用Selenium Python绑定读取div中的隐藏值 [英] Read a hidden value in a div using Selenium Python binding

查看:381
本文介绍了使用Selenium Python绑定读取div中的隐藏值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:如何在嵌套的<div>中读取值?

Issue: How can you read a value in a nested <div>?

情况:我正在我们的网站上测试注册.为了完成该过程,用户将需要输入验证码.使用帮助程序类(在我们的开发服务器上),我们可以显示(隐藏)当前的验证码.我需要抓住它并保存以供.send_keys函数使用(该部分是简单的部分).我没有任何代码可以分享我正在使用的内容,因为我什至不知道该跳转到哪里.

Situation: I am testing the registration on our web site. In order to complete the process, the user will need to enter the captcha. Using a helper class (on our dev server) we can display (hidden) the current captcha. I need to grab that and save it to be used by a .send_keys function (that part is the easy part). I have no code to share with what I am using, as I am not even sure where to jump in.

好吧,我有这个,但这显然不是答案:

Well, I have this, but this is obviously not the answer:

driver.find_element_by_id("capchta").send_keys("foobar")

通过上面的代码,我可以发送一个键值(称为"foobar").我只需要用在value=XXX

With above, I can send a key value (seen as "foobar"). I just need to replace "foobar" with a string that I read in the value=XXX

这是带有验证码的HTML代码,用value=XXXX表示.

Here is the HTML code with the captcha, represented with the value=XXXX.

<div id="code_area">
     <p id="captcha_blurb">Please enter the security code below</p>
     <div id="capchta_area">
     <img id="secuity_icon" src="/img3/Secuity-Icon.png">
     <img id="security-image" src="data:image/png;==">
     <input id="current_captcha" type="hidden" value="XXXX">
     <input id="capchta" class="reg_form_input" value="" name="code" placeholder="Enter Security Code" title=" Please Enter These Characters." onblur="removeInputInfoBox('reg_box');" onfocus="addInputInfoBox('#capchta','#capchta_area', 'reg_box');">
</div>

推荐答案

我认为您无需执行JS即可获取隐藏输入的值.

I don't think you need to execute JS to get the hidden input's value.

您可以将get_attribute用作值属性

get_attribute(name)

因此,在您的情况下,请尝试:

So in your case, try:

captcha_value = driver.find_element_by_id("current_captcha").get_attribute("value")
driver.find_element_by_id("capchta").send_keys(captcha_value)

这篇关于使用Selenium Python绑定读取div中的隐藏值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆