使用Selenium和Python定位,在文本框中输入值

时间:2021-08-27 23:51:03
<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>

I have the above code and I am trying to use selenium to enter a value of NUM above Here is the code I have written:

我有上面的代码,我正在尝试使用selenium输入值NUM以上这是我写的代码:

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?

如何获取此元素并输入值?

1 个解决方案

#1


66  

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() 

#1


66  

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()