Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to locate and insert a value in a text box (input) using Python Selenium?

I have the following HTML structure and I am trying to use Selenium to enter a value of 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‎‎‎‎‎‎‎? ‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎

like image 227
cppb Avatar asked Sep 01 '13 09:09

cppb


People also ask

How do you enter a value in a text box in Python?

We can send keyboard input to a textbox on a webpage in Selenium webdriver in Python using the method send_keys. The text to be entered is passed as a parameter to that method. To perform keyboard actions, we can also use the send_keys method and then pass the class Keys.

How will you enter a value in a text box using Selenium Webdriver?

We can type in a textbox using Selenium webdriver. We shall use the sendKeys() method to type in the edit box. It is an in-built method in Selenium. Let us consider a text box where we shall enter some text.

How do I find an element that contains specific text in Selenium Webdriver Python?

We can find an element that contains specific text with Selenium webdriver in Python using the xpath. This locator has functions that help to verify a specific text contained within an element. The function text() in xpath is used to locate a webelement depending on the text visible on the page.


1 Answers

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()  
like image 106
zero323 Avatar answered Sep 24 '22 09:09

zero323