Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does selenium write and read the webelement values

I am doing automation using Python2.7 and selenium first time. Now can I write and read as well the below HTML contents?

Radio Buttons

<form name="myWebForm" action="mailto:[email protected]" method="post">
<h4>Please select your favorite food category.</h4>
<input type="radio" name="food" /> : Italian<br />
<input type="radio" name="food" /> : Greek<br />
<input type="radio" name="food" /> : Chinese<br />
<h4>Please select your gender.</h4>
<input type="radio" name="gender" /> : Male<br />
<input type="radio" name="gender" /> : Female<br />
</form>

Single Select List

  <select size="3" name="selectionField" multiple="yes" > 
      <option value="CA" >California -- CA </option>
      <option value="CO" >Colorado -- CO</option>
      <option value="CN" >Connecticut -- CN</option>
    </select>

Defination List

<dl>
<dt>Coffee</dt>
<dd>- black hot drink</dd>
<dt>Milk</dt>
<dd>- white cold drink</dd>
</dl> 

CheckBoxes

<form name="myWebForm" action="mailto:[email protected]" method="post">
<p>Please select every sport that you play.</p>

Soccer: <input type="checkbox" name="sports" value="soccer"  /><br />

Football: <input type="checkbox" name="sports" value="football"  /><br /> 

Baseball: <input type="checkbox" name="sports" value="baseball"  /><br /> 

Basketball: <input type="checkbox" name="sports" value="basketball"  />

</form>
like image 323
CodeLover Avatar asked Jan 07 '13 19:01

CodeLover


People also ask

What is the use of WebElement in Selenium?

Each WebElement is represented in Selenium via the WebElement interface – which is used by Selenium to interact with visible and invisible elements on the web page. This command returns either the element being searched for or returns null/void.

How can you read a text in a web element using Selenium?

The Selenium WebDriver interface has predefined the getText() method, which helps retrieve the text for a specific web element. This method gets the visible, inner text (which is not hidden by CSS) of the web-element.

How do you find the value of an element in Selenium?

The getAttribute() method helps to get the value of any attribute of a web element, which is returned as a String. If an attribute has a Boolean value, the method returns either True or null. Also, if there is no attribute, the method will return null.


1 Answers

Yes, check into xpath

x = brower.select_element_by_xpath('//option[contains(text(), "CO"]')
x.text (print the div text) 
x.click() clicks the div

or just

brower.select_element_by_xpath('//option[contains(text(), "CO"]').click()

to read the list, this should work;

for i in browers.select_elements_by_xpath('//select[@name="selectionField"]//option'):
    print i.text

you can select list, radio buttons, span's whatever, just learn Xpath it's worth the effort.

like image 199
Chris Hawkes Avatar answered Oct 04 '22 13:10

Chris Hawkes