Python selenium didn't find css element
Question: I imported the code from Selenium Ide in python. The selenium test works fine
without clicks on the item and scroll seamlessly clicks on an item. HTML
selenium code :

    
    
        <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head profile="http://selenium-ide.openqa.org/profiles/test-case">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="selenium.base" href="http://www.amazon.com/" />
    <title>New Test</title>
    </head>
    <body>
    <table cellpadding="1" cellspacing="1" border="1">
    <thead>
    <tr><td rowspan="1" colspan="3">New Test</td></tr>
    </thead><tbody>
    <tr>
        <td>open</td>
        <td>/</td>
        <td></td>
    </tr>
    <tr>
        <td>clickAndWait</td>
        <td>css=[alt=&quot;Deals in Books&quot;]</td>
        <td></td>
    </tr>
    
    </tbody></table>
    </body>
    </html>
    

But in Python does not work until you scroll-click to the to the desired item.

    
    
    from selenium import webdriver
    import unittest, time, re
    
    class Untitled(unittest.TestCase):
        def setUp(self):
            self.driver = webdriver.Firefox()
            self.driver.implicitly_wait(30)
            self.base_url = "http://www.amazon.com/"
            self.verificationErrors = []
            self.accept_next_alert = True
    
        def test_untitled(self):
            driver = self.driver
            driver.get(self.base_url)
            driver.find_element_by_css_selector("a.feed-carousel-control.feed-right > span.gw-icon.feed-arrow").click()
            driver.find_element_by_css_selector("a.feed-carousel-control.feed-right > span.gw-icon.feed-arrow").click()
            driver.find_element_by_css_selector("a.feed-carousel-control.feed-right > span.gw-icon.feed-arrow").click()
            driver.find_element_by_css_selector("a.feed-carousel-control.feed-right > span.gw-icon.feed-arrow").click()
            driver.find_element_by_css_selector("[alt=\"Deals in Books\"]").click()
    
      def tearDown(self):
            self.driver.quit()
            self.assertEqual([], self.verificationErrors)
    

I tested in different selenium locators and xpath css and it works, but in
python without scrolling click on the element is not working.


Answer: First, you may [wait for the element to become visible](https://selenium-
python.readthedocs.org/waits.html):

    
    
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    element = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, "[alt=\"Deals in Books\"]"))
    )
    element.click()
    

And, if you need to scroll to the element before clicking it, use the [Action
Chains](https://selenium-python.readthedocs.org/api.html#module-
selenium.webdriver.common.action_chains):

    
    
    deals_in_books = driver.find_element_by_css_selector("[alt=\"Deals in Books\"]")
    
    actions = ActionChains(driver)
    actions.move_to_element(deals_in_books).click().perform()
    

And, if needed, [scroll into view of the
element](https://developer.mozilla.org/en-
US/docs/Web/API/Element/scrollIntoView):

    
    
    browser.execute_script("arguments[0].scrollIntoView();", deals_in_books)
    

