Home > Software design >  I am scraping data with Selenium from FLIPKART.COM ecommerce site. I am unable to click on Read More
I am scraping data with Selenium from FLIPKART.COM ecommerce site. I am unable to click on Read More

Time:05-23

I want to click on above mentioned elements and scrape the data. link1 and link2

Correct Element: Xpath of the element = //*[@id="container"]/div/div[3]/div[1]/div[2]/div[8]/div[4]/div/div[2]/button

When I pass this path, I am getting this error:

NoSuchElementException: Message: no such element: Unable to locate element:

My code:

driver = webdriver.Chrome('chromedriver.exe')
driver.get('http://www.flipkart.com/')

close_login_popup = driver.find_element_by_xpath('/html/body/div[2]/div/div/button')
close_login_popup.click()

search_mobile = driver.find_element_by_class_name("_3704LK")
search_mobile.send_keys('Oneplus Nord',Keys.ENTER)

Container which contains all the data about the product.

container = driver.find_elements_by_class_name("_4ddWXP")

clicking on product name

click_product = data.find_element(By.XPATH, './/a[@]').click()

clicking on read more -- this is not working

click_readMore = data.find_element(By.CLASS_NAME,"_2KpZ6l _1FH0tX").click()

Alternative codes I have tried to click on Read More button

click_readMore = .find_element_by_xpath('/html/body/div[1]/div/div[3]/div[1]/div[2]/div[8]/div[4]/div/div[2]/button').click()

driver.find_element_by_xpath('//button[@]')

driver.find_element(By.XPATH, '//div[@]//button')````

CodePudding user response:

"Unable to locate element:" it means it can't find the element because it's a little further down the page, you need to scroll, you can try different numbers, 200, 300, 400, etc. in this case it worked for me with 900 px.

and add this options on webdriver

    option = webdriver.ChromeOptions()
    option.add_argument("--disable-extensions")
    option.add_argument('user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36')
    option.add_argument("window-size=1500,1200")
    driver = webdriver.Chrome('chromedriver.exe', chrome_options=option)

and then scroll to element

try:
    driver.execute_script("window.scrollTo(0, 200);")
    el = driver.find_element(by=By.XPATH, value="//button[normalize-space()='Read More']").click()
except: 
    try:
        driver.execute_script("window.scrollTo(0, 300);")
        el = driver.find_element(by=By.XPATH, value="//button[normalize-space()='Read More']").click()
    except: pass

CodePudding user response:

Please try this //button[translate(text(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='read more']

  • Related