如何使用 Python Selenium 在文本框(输入)中定位和插入值?

我有以下 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>

下面是我写的代码:

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)

如何获取这个元素并输入一个值

254008 次浏览

Assuming your page is available under "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:

inputElement = driver.find_element_by_id("a1")
inputElement.send_keys('1')

Now you can simulate hitting ENTER:

inputElement.send_keys(Keys.ENTER)

or if it is a form you can submit:

inputElement.submit()
web=driver.get('your web address')
input_1=driver.find_element(by=By.ID, value= 'id of element')
input_2=driver.find_element(by=By.ID, value= 'id of element')
input_3=driver.find_element(by=By.ID, value= 'id of element')


time.sleep(7)
input_1.send_keys(your value)
input_2.send_keys(your value)
input_3.send_keys(your value)