Sunday, February 8, 2015

Python Selenium Notes

Python Selenium notes:

Case 1:
Sometimes identified the element using xpath works fine with the selenium IDE, but not with the webdriver ( or via python script)

Solution:
Then its not the problem of the xpath element.

The problem with the webpage framing.We should move to the respective frame first and then use the xpath.

Eg;
driver.switch_to_frame(driver.find_element_by_tag_name("iframe"))
driver.find_element_by_xpath("ENTER THE XPATH HERE")
-----------------------------------------------------------------------------------------------------
Case 2:
After case 1, how to back to the default frame to access the other elements?

Solution:
driver.switch_to_default_content()
-------------------------------------------------------------------------------------------------------
Case 3:
Mouse over command in python

Solution:

product = driver.find_element_by_link_text("Products")
Hover = ActionChains(driver).move_to_element(product)
Hover.perform()
-------------------------------------------------------------------------------------------------------
Case 4:
Access variable of one function from another function in a class without using __init__ method

Solution with example:

class Testclass():
    #a = "vallinayagam"
 
    def method_one(self):
        a = "valli"
        return a
     

    def method_two(self):
        print self.method_one()
     
ba = Testclass()
ba.method_two()
------------------------------------------------------------------------------------------------------
Case 5:
Xpath filter commands:

For below HTML,






in the <td, we can access the values for class, data-year, data-month using below command

1. First identify the xpath  by rightclick -> Copy XML and filter t using "data-month]

 xpath path  =//div[5]/div/table/tbody/tr/td[5]
 Filter = [@data-month='2']

To locate element using xpath = "xpath=//div[5]/div/table/tbody/tr/td[5][@data-month='2']"

Case 6:
How to select a value from dropdown box?








Solution:
Step 1: Identify the ID and value from the options tag. you want?

code:
set = driver.find_element_by_id('ctl00_ctl00_body_PrintContent_IWOVID_item_1_ddlCountries')

for option in set.find_elements_by_tag_name('option'):
if option.text == 'India':
option.click()
break




No comments:

Post a Comment