How can I select the "Sort By" element through Selenium Webdriver Python
<p>I was trying to automate one task using Selenium Webdriver with Python 2.7.The task goes as follows:
1. Open "<a href="https://www.flipkart.com" rel="nofollow">https://www.flipkart.com</a>".
2. Search for 'laptop'.
3. Hit search button.
4. Whatever is the answer of the search query, sort them "By Popularity" using the sort button.</p>

<p>Code</p>

<pre><code>from selenium import webdriver

url="https://www.flipkart.com"
xpaths={'submitButton' :   "//button[@type='submit']",
    'searchBox' : "//input[@type='text']"}

driver=webdriver.Chrome()
driver.maxmimize_window()

driver.get(url)

#to search for laptops
driver.find_element_by_xpath(xpaths['searchBox']).clear()
driver.find_element_by_xpath(xpaths['searchBox']).send_keys('laptop')
driver.find_element_by_xpath(xpaths['submitButton']).click()

#to sort them by popularity
driver.find_element_by_xpath("////*[@id='container']/div/div[2]/div[2]/div/div[2]/div[2]/div/section/ul/li[2]").click()
</code></pre>

<p>The last statement throws an error:</p>

<pre><code>raise exception_class(message, screen, stacktrace)
InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression ////*[@id='container']/div/div[2]/div[2]/div/div[2]/div[2]/div/section/ul/li[2] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '////*[@id='container']/div/div[2]/div[2]/div/div[2]/div[2]/div/section/ul/li[2]' is not a valid XPath expression.
  (Session info: chrome=53.0.2785.143)
  (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.3 x86_64)
</code></pre>

<p>Given I copied the xpath for that particular element "Sort By - Popularity" using the Chrome developer tools(ctrl+shift+I).</p>

<p>And also, it highlights the same element when I try to search for that xpath in the console window of developer tools.</p>

<p>What's wrong with the xpath?
Help!</p>

<p>You may try with this css selector too:</p>

<pre><code>#container &gt; div &gt; div:nth-child(2) &gt; div:nth-child(2)  &gt; div &gt; div:nth-child(2)  &gt; div:nth-child(2)  &gt; div &gt; section &gt; ul &gt; li:nth-child(2)
</code></pre>

<p>To me, cssSelector is better than xpath for browser compatibility too.</p>

python selenium xpath