Monday, 17 March 2014

CssSelector in Selenium Webdriver in Python - Example

CssSelector is another standard to find the elements from the webpage. We should prefer css selectors over xpath to identify the elements from the webpage as it works very well in all browsers.

Below table shows commonly used css Selectors in Selenium.


Find all elements with tag input

input

Find all input tag element having  attribute type = ‘hidden’

input[type='hidden']

Find all input tag element having  attribute type = ‘hidden’  and name attribute = ‘ren’

input[type='hidden'][name='ren']

Find all input tag element with attribute type containing ‘hid’

input[type*='hid']

Find all input tag element with attribute type starting with ‘hid’

input[type^='hid']

Find all input tag element with attribute type ending with ‘den’

input[type$='den']


Below example demonstrates how we can use cssSelectors to identify the elements in Python.


from selenium import webdriver

#driver = webdriver.Chrome("F:\\selenium\\Csharp\\iedriverserver.exe")

#driver = webdriver.Chrome("F:\\selenium\\Csharp\\chromedriver.exe")

driver = webdriver.Firefox()

driver.get("http://register.rediff.com/register/register.php")

try:
    driver.find_element_by_css_selector("input[@type='password']").send_keys("password")

driver.close()

except Exception as e:
    print ("Exception occured", format(e));

finally:
    driver.quit()

    print ("finally")

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

No comments:

Post a Comment

Buy Best Selenium Books