0

I am trying to get the Add Ons from this page. If the item is customisable.

https://www.swiggy.com/restaurants/imperial-restaurant-shivajinagar-central-bangalore-bangalore-48556

After collecting all the divs, I try to go to the top of the page(Doesn't work). I check if the customisable div is present. If so, scroll it into view, click the Add button and get Add on details. I have tried, actionChains, didn't work. execute_script also does nothing. The click was working before but the scrolling up and down the page kind of messed it up. Been trying to get this right for hours. Any help is greatly appreciated.

    for i in range(6):
        item_dvs = driver.find_elements_by_class_name('_2wg_t')
        driver.execute_script("window.scrollBy(0, 3100)")
    driver.execute_script("window.scrollBy(0,-18500);")
    for div in item_dvs:
        try:
            name = div.find_element_by_class_name('styles_itemNameText__3bcKX')
            price = div.find_element_by_class_name('rupee')
            if div.find_elements_by_class_name('styles_itemDesc__MTsVd'):
                desc = div.find_element_by_class_name('styles_itemDesc__MTsVd').text
            else:
                desc = None
            is_cust = div.find_element_by_class_name('_1C1Fl _23qjy')
            if is_cust:
                driver.execute_script("arguments[0].scrollIntoView();", is_cust)

1 Answer 1

1

First of all, I suggest you to use WebDriverWait because the lines if div.find_elements_by_class_name('styles_itemDesc__MTsVd') and is_cust = div.find_element_by_class_name('_1C1Fl _23qjy') will throw you an exception when the elements don't exist. Another reason to use WebDriverWait is to wait for the Add button to be clickable. Here a solution to both problems where you can use try/catch to handle the existence of the description and the "customisable" element and you can wait for the Add button to be clickable:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import TimeoutException


driver = webdriver.Chrome()
driver.get("https://www.swiggy.com/restaurants/imperial-restaurant-shivajinagar-central-bangalore-bangalore-48556")
wait = WebDriverWait(driver, 10)

try:
    item_dvs = []
    for i in range(6):
        item_dvs = wait.until(ec.visibility_of_all_elements_located((By.CLASS_NAME, '_2wg_t')))
        driver.execute_script("window.scrollBy(0, 3100)")
    driver.execute_script("window.scrollBy(0,-18500);")

    for div in item_dvs:
        wait_div = WebDriverWait(div, 10)
        name = wait_div.until(ec.visibility_of_element_located((By.CLASS_NAME, 'styles_itemNameText__3bcKX')))
        price = wait_div.until(ec.visibility_of_element_located((By.CLASS_NAME, 'rupee')))

        try:
            desc = wait_div.until(ec.visibility_of_element_located((By.XPATH, ".//div[contains(@class, 'styles_itemDesc__MTsVd')]"))).text
        except TimeoutException:
            desc = None

        try:
            is_cust = wait_div.until(ec.visibility_of_element_located((By.XPATH, ".//div[contains(@class, '_1C1Fl _23qjy')]")))
            add_on = wait_div.until(ec.element_to_be_clickable((By.XPATH, ".//div[contains(@class, 'F8dpS _3L1X9')]")))
            ActionChains(driver).move_to_element(add_on).click().perform()
            # get add on details
            # close add window
        except TimeoutException:
            pass

    driver.close()

except Exception as e:
    driver.close()
    raise e
Sign up to request clarification or add additional context in comments.

4 Comments

Doesn't work man. Doesn't click the add on.
How much have you waited? Because for the first element the driver doesn't find the description so you have to wait at least 10 seconds. I tried the code by my own and it works fine. For add button do you mean the button at .//div[contains(@class, 'F8dpS _3L1X9')] right?
It wasn't a question of wait. This code was working fine. I fixed it now. Multiple class name will not be found. I should have used xpath. is_cust = div.find_element_by_xpath('//div[@class="_1C1Fl _23qjy"]'). This works. Many thanks for your help, though.
Yes sure, I wrote it also in the answer with that xpath. If you want mark the answer as the right one for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.