dimanche 9 octobre 2016

Selecting checkboxes using selenium web driver with python

I am trying to perform a test on Amazon.com categories checkboxes using selenium web driver with python code, and I tried several ways but I am not sure how to select the checkbox of the Book category for example without having its id or name. I used some plugins to get the right xpath like XPath Helper for chrome and path checker on Firefox...

Here is my code and my tries are commented.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from time import sleep
import unittest

class Test_PythonOrg(unittest.TestCase):

    def setUp(self):
        self.browser = webdriver.Firefox()

    def tearDown(self):
        sleep(4)
        self.browser.close()
        self.browser = None


    def test_07_ListOfTodayDeals(self):
        self.browser.get("http://www.amazon.com")
        deals = self.browser.find_element_by_link_text("Today's Deals")
        deals.click()
        books = self.browser.find_element_by_id("widgetFilters")
        books.find_element_by_xpath("/x:div[1]/x:div/x:span[8]/x:div/x:label/x:span").click()

        #for i in range(20):
            #try:
                #self.browser.find_element_by_xpath(".//*[contains(text(), 'Books')]").click()
                #break
            #except NoSuchElementException as e:
                #print('retry')
                #time.sleep(1)
        #else:
            #print('Test Failed')

        browser.close()
        #sign_in_button = self.browser.find_element(By_XPATH,'(//input[@name=''])[8]')
        #sign_in_button.find_element_by_xpath("//select[@name='element_name']/option[text()='option_text']").click()
        #sleep(5)
        #self.assertTrue("No results found." not in self.browser.page_source)



if __name__ == "__main__":
    unittest.main(verbosity=2)




Aucun commentaire:

Enregistrer un commentaire