如何使用 Python Selenium 在文本框(输入)中定位和插入值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18557275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to locate and insert a value in a text box (input) using Python Selenium?
提问by cppb
I have the following HTML structure and I am trying to use Selenium to enter a value of NUM
:
我有以下 HTML 结构,我正在尝试使用 Selenium 输入值NUM
:
<div class="MY_HEADING_A">
<div class="TitleA">My title</div>
<div class="Foobar"></div>
<div class="PageFrame" area="W">
<span class="PageText">PAGE <input id="a1" type="txt" NUM="" /> of <span id="MAX"></span> </span>
</div>
Here is the code I have written:
这是我写的代码:
head = driver.find_element_by_class_name("MY_HEADING_A")
frame_elem = head.find_element_by_class_name("PageText")
# Following is a pseudo code.
# Basically I need to enter a value of 1, 2, 3 etc in the textbox field (NUM)
# and then hit RETURN key.
## txt = frame_elem.find_element_by_name("NUM")
## txt.send_keys(Key.4"
How to get this element and enter a value?
如何获取此元素并输入值?
采纳答案by zero323
Assuming your page is available under "http://example.com"
假设您的页面在“ http://example.com”下可用
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://example.com")
Select element by id:
按 id 选择元素:
inputElement = driver.find_element_by_id("a1")
inputElement.send_keys('1')
Now you can simulate hitting ENTER:
现在您可以模拟按 ENTER 键:
inputElement.send_keys(Keys.ENTER)
or if it is a form you can submit:
或者如果是表格,您可以提交:
inputElement.submit()