0

I am trying to automate a dropdown menu that has hours, and my goal is always choose one hour ahead of a current time.

from selenium import webdriver
from selenium.webdriver.remote.webdriver import WebDriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from LoginPage import executor_url,session_id
from datetime import date
from datetime import datetime,timedelta

def attach_to_session(executor_url, session_id):
    original_execute = WebDriver.execute
    def new_command_execute(self, command, params=None):
        if command == "newSession":
            # Mock the response
            return {'success': 0, 'value': None, 'sessionId': session_id}
        else:
            return original_execute(self, command, params)
    # Patch the function before creating the driver object
    WebDriver.execute = new_command_execute
    driver = webdriver.Remote(command_executor=executor_url, desired_capabilities={})
    driver.session_id = session_id
    # Replace the patched function with original function
    WebDriver.execute = original_execute
    return driver

bro = attach_to_session(executor_url, session_id)
bro.get('https://heregoesmywebportaladdress')
today=date.today()
now=datetime.now()+ timedelta(hours=1)
print(date.strftime(now,'%H'))
bro.find_element_by_id("snapshot_scheduler_form_startDate").send_keys(date.strftime(today, '%Y-%m-%d'))
time.sleep(2)
bro.find_element_by_id("snapshot_scheduler_form_startTime_hour").click()

THIS IS WHERE I HAVE NO CLUE HOW TO DO IT.

bro.find_element_by_xpath("/html/body/div[2]/div/div[2]/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/div/div[2]/form/fieldset[1]/div/select[1]/option[13]").click()
time.sleep(2)

these are the options

<select id="snapshot_scheduler_form_startTime_hour" name="snapshot_scheduler_form[startTime][hour]" class="form-control">
<option value="0">00</option>
<option value="1">01</option>
<option value="2">02</option>
<option value="3">03</option>
<option value="4">04</option>
<option value="5">05</option>
<option value="6">06</option>
<option value="7">07</option>
<option value="8">08</option>
<option value="9">09</option>
<option value="10">10</option> ETCD
Sers
  • 12,047
  • 2
  • 12
  • 31
  • Pavel is better to edit you question, not to post here in comments. Also is not recomendable use that kind of xpath, try to get element by class, id or something like that, not right click get xpath. Usually you have you have to click the input element (to open dropdown) and then click your desired option – Wonka Jan 08 '20 at 16:42
  • Its not a simple selection from a dropdown, i have a code that is getting the current time +1 hour and need that mapped to an option from dropdown menu. – Pavel Gusilic Jan 08 '20 at 16:46
  • _mapped to an option from dropdown menu._ You want to select the option whose value is the current time + 1 hour? – AMC Jan 08 '20 at 17:03
  • @AMC yes correct – Pavel Gusilic Jan 08 '20 at 17:12
  • I just found how to do it. Thanks Wonka for telling my mistake on xpath. so here is how i did it. now1=date.strftime(now,'%H') Select(bro.find_element_by_id("snapshot_scheduler_form_startTime_hour")).select_by_visible_text(now1) – Pavel Gusilic Jan 08 '20 at 17:20

1 Answers1

0

The dropdown is a select element. You can use Selenium Select for that, here is another example:

from selenium.webdriver.support.select import Select

# select by value
Select(driver.find_element_by_id("myid")).select_by_value(hour)

# or select by text
Select(driver.find_element_by_id("myid")).select_by_visible_text()
Sers
  • 12,047
  • 2
  • 12
  • 31