Selecting checkboxes using selenium web driver with python
<p>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... </p>

<p>Here is my code and my tries are commented. </p>

<pre><code>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)
</code></pre>

<p>HTML</p>

<pre><code>&lt;span class="a-declarative" data-action="gbfilter-checkbox" data-gbfilter-checkbox="{&amp;quot;attribute&amp;quot;:&amp;quot;whitelist_categories&amp;quot;,&amp;quot;value&amp;quot;:&amp;quot;283155&amp;quot;,&amp;quot;rangeEnd&amp;quot;:&amp;quot;&amp;quot;,&amp;quot;rangeStart&amp;quot;:&amp;quot;&amp;quot;,&amp;quot;filterType&amp;quot;:&amp;quot;checkboxes&amp;quot;}"&gt;

                &lt;div class="a-checkbox checkbox checked a-spacing-micro"&gt;&lt;label&gt;&lt;input name="" value="" checked="" type="checkbox"&gt;&lt;i class="a-icon a-icon-checkbox"&gt;&lt;/i&gt;&lt;span class="a-label a-checkbox-label"&gt;


                        Books
                &lt;/span&gt;&lt;/label&gt;&lt;/div&gt;

        &lt;/span&gt;
</code></pre>

<p>I have modified your code and now it's working</p>

<pre><code>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()
        sleep(5)
        self.browser.find_element_by_xpath("//span[@class='a-label a-checkbox-label' and contains(text(),'Books')]/preceding-sibling::input[1]").click()
        self.browser.close()


if __name__ == "__main__":
    unittest.main(verbosity=2)
</code></pre>

python selenium xpath checkbox