Home > OS >  How to select a option value, click in another menu and then hit submit
How to select a option value, click in another menu and then hit submit

Time:03-09

I have this page with two menus and a submit button. I would like to select an option in the first menu (companies), then select an item in the second menu (type) and finally hit the submit button (Send)

Here is the simplified HTML page:

                             <select name="companies" multiple="multiple" id="IDcompanies" style="width:200px;">
    <option value="01">Facebook</option>
    <option value="02">Oracle </option>
    <option value="03">AWS</option>
    <option value="04">Tesla</option>
    </select>
    
    
                             <select name="type"  id="IDtype" style="width:200px;">
    <option value="T1">Type1 </option>
    <option value="T2">Type2 </option>
    <option value="T3">Type3</option>
    </select>
                     

    <input type="submit" name="Button1" value="Send" id="ID_Button1" />

In python, I'm trying this to the first part: click on the first company (Facebook):

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By

PATH = "C:\Program Files (x86)\chromedriver.exe"

driver = webdriver.Chrome(PATH)

driver.get(url)

box = driver.find_element(By.ID, "IDcompanies")

box.select_by_index(0)

But gives me an error: AttributeError: 'WebElement' object has no attribute 'select_by_index'

I appreciate if someone can help with this error and guide me on how to proceed to make this sequence of clicking on the first menu, then on the second and then on the submit button.

CodePudding user response:

In order to use special Selenium methods like select_by_index, select_by_value and select_by_visible_text you should define and initialize the special Selenium Select object, as following:

companies_select = Select(driver.find_element(By.ID, "IDcompanies"))

companies_select.select_by_index(0)

For more details see here

  • Related