简体   繁体   中英

How can I select the “Sort By” element through Selenium Webdriver Python

I was trying to automate one task using Selenium Webdriver with Python 2.7.The task goes as follows: 1. Open " https://www.flipkart.com ". 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.

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()

The last statement throws an error:

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)

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

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

What's wrong with the xpath? Help!

As far as syntax error goes replace //// with // in your xpath

driver.find_element_by_xpath("//*@id='container']/div/div[2]/div[2]/div/div[2]/div[2]/div/section/ul/li[2]").click()

This will resolve your syntax error and will do the required job Though In my opinion a better XPATH would be driver.find_element_by_xpath("//li[text()='Popularity']").click()

you can use following code.

from selenium import webdriver
import time
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
time.sleep(5)
driver.find_element_by_xpath("//li[text()='Popularity']").click()

You may try with this css selector too:

#container > div > div:nth-child(2) > div:nth-child(2)  > div > div:nth-child(2)  > div:nth-child(2)  > div > section > ul > li:nth-child(2)

To me, cssSelector is better than xpath for browser compatibility too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM