Skip to content

Instantly share code, notes, and snippets.

@mrkhedri
Last active January 12, 2022 06:53
Show Gist options
  • Select an option

  • Save mrkhedri/586908864842ecf3355bdc324acf1d3a to your computer and use it in GitHub Desktop.

Select an option

Save mrkhedri/586908864842ecf3355bdc324acf1d3a to your computer and use it in GitHub Desktop.
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
remote_driver_url = r'http://127.0.0.1:4444/wd/hub' # remote selenium driver
url = r'' # TODO: charisma web reserve url
username = '' # TODO: personnel code
password = '' # TODO: personnel code
favorite_foods = ['سالاد', 'بشقاب سبزيجات'] # foods start with ..?
driver = webdriver.Remote(remote_driver_url, DesiredCapabilities.CHROME)
driver.get(url)
time.sleep(1)
# Login
driver.find_element(By.XPATH, '//*[@id="txtUserName"]').send_keys(username)
driver.find_element(By.XPATH, '//*[@id="txtPassword"]').send_keys(password)
driver.find_element(By.XPATH, '//*[@id="btnLogin"]').click()
time.sleep(1)
# Go to next week page
driver.find_element(By.XPATH, '//*[@id="ctl00_ContentPlaceHolder1_btnNextWeek"]').click()
time.sleep(3)
def get_day_id(day_index):
return f'//*[@id="ctl00_ContentPlaceHolder1_reservedGrid_ctl00__{day_index}"]'
for index in list(range(5)):
day_xPath = get_day_id(index)
foods_select_id = 'ctl00_ContentPlaceHolder1_ddlFood'
food_submit_xPath = '//*[@id="ctl00_ContentPlaceHolder1_btnSubmit"]'
# click a day
driver.find_element(By.XPATH, day_xPath).click()
time.sleep(1)
# select my favorite foods
html = driver.execute_script('return document.documentElement.outerHTML')
options = BeautifulSoup(html,'html.parser').select(f'#{foods_select_id} option')
favorit_food_index = -1
i = 0
for option in options:
food = option.get_text()
for item in favorite_foods:
if food.startswith(item):
favorit_food_index = i
i += 1
if favorit_food_index > -1:
select = Select(driver.find_element_by_id(foods_select_id))
select.select_by_index(favorit_food_index)
time.sleep(0.5)
driver.find_element(By.XPATH, food_submit_xPath).click()
time.sleep(1)
driver.quit()
services:
selenium:
image: selenium/standalone-chrome
container_name: selenium
ports:
- 4444:4444
networks:
- share-network
environment:
- SE_NODE_MAX_SESSIONS=10
- SE_NODE_OVERRIDE_MAX_SESSIONS=true
volumes:
- ./path/to/downloads:/home/seluser/Downloads
networks:
share-network:
external: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment