Asked  7 Months ago    Answers:  5   Viewed   52 times

I need to select an element from a drop-down menu.

For example:

<select id="fruits01" class="select" name="fruits">
  <option value="0">Choose your fruits:</option>
  <option value="1">Banana</option>
  <option value="2">Mango</option>

1) First I have to click on it. I do this:

inputElementFruits = driver.find_element_by_xpath("//select[id='fruits']").click()

2) After that I have to select the good element, lets say Mango.

I tried to do it with inputElementFruits.send_keys(...) but it did not work.



Unless your click is firing some kind of ajax call to populate your list, you don't actually need to execute the click.

Just find the element and then enumerate the options, selecting the option(s) you want.

Here is an example:

from selenium import webdriver
b = webdriver.Firefox()

You can read more in:

Tuesday, June 1, 2021
answered 7 Months ago

If you intend to use Selenium in Grid configuration through Hub and Node configuration, I would suggest you to use the most recent selenium-server-standalone-3.6.0 jar as follows:

  1. Start the Selenium Grid Hub (by default on port 4444) :

    java -jar selenium-server-standalone-3.6.0.jar -role hub
  2. Confirm the Selenium Grid Hub is started:

    16:06:29.891 INFO - Nodes should register to
    16:06:29.891 INFO - Selenium Grid hub is up and running
  3. Access the Selenium Grid Hub Console and ensure Selenium Grid Hub is up and running:

  4. Start the Selenium Grid Node (by default on port 5555) for Mozilla/GeckoDriver:

    java -Dwebdriver.gecko.driver=geckodriver.exe -jar selenium-server-standalone-3.6.0.jar -role node -hub http://localhost:4444/grid/register
  5. Confirm the Selenium Grid Node is registered and started:

    16:15:54.696 INFO - Selenium Grid node is up and ready to register to the hub
    16:15:54.742 INFO - Starting auto registration thread. Will try to register every 5000 ms.
    16:15:54.742 INFO - Registering the node to the hub: http://localhost:4444/grid/register
    16:15:54.975 INFO - The node is registered to the hub and ready to use
  6. Execute with the Testcase with DesiredCapabilities as follows:

    self.driver = webdriver.Remote(command_executor='', desired_capabilities=caps)
  7. Observe the console logs ending with the following on successful execution of your Testcase:

    16:23:50.590 INFO - Found handler: org.openqa.selenium.remote.server.ServicedSession@37ff9771
    16:23:50.590 INFO - Handler thread for session 31a1dcb0-8bed-40fb-acdb-d5be19f03ba2 (firefox): Executing DELETE on /session/31a1dcb0-8bed-40fb-acdb-d5be19f03ba2
     (handler: ServicedSession)
    1506941630595   Marionette      INFO    New connections will no longer be accepted
Tuesday, June 15, 2021
answered 6 Months ago

If you want a value and a caption (label), create an appropriate class

class ComboItem
    public int ID { get; set; }
    public string Text { get; set; }

In the ComboBox you then set the DisplayMember property to Text and the ValueMember property to ID.

The DropDownStyle of the ComboBox determines its behavior. DropDownStyle.DropDown enables the user to type in text. With DropDownStyle.DropDownList the user can only select items from the list.

You can fill the ComboBox like this:

myCombo.DataSource = new ComboItem[] {
    new ComboItem{ ID = 1, Text = "One" },
    new ComboItem{ ID = 2, Text = "Two" },
    new ComboItem{ ID = 3, Text = "Three" }

The DataSource can be any kind of enumerable.

You can retrieve the selected ID like this

int id = (int)myComboBox.SelectedValue;

Note that you can add any type of item to the ComboBox. If you don't specify the DisplayMember and ValueMember properties, the ComboBox uses the ToString method of the object to determine the text displayed and you can retrieve the selected item (not selected value) through the SelectedItem property.

If you add objects of this type ...

class Person
    public int PersonID { get; set }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public override string ToString()
        return FirstName + " " + LastName;
 } the ComboBox, you can retrieve the selected item like this

Person selectedPerson = (Person)myComboBox.SelectedItem;
int personID = selectedPerson.PersonID;

The ComboBox will display the first and last names of the persons.

Tuesday, August 3, 2021
answered 4 Months ago

Css does not allow you do text based search. xpath is the only option there.

Tuesday, August 10, 2021
answered 4 Months ago

Create a new user profile on your browser, activate it and login to using the newly created profile, close the browser. Run the python script and initiate the webdriver using the new profile and you should still be logged in, i.e.:

The example below is for Firefox and, but the general concept can be used on other browsers and websites.

1 - Type about:profiles on the browser url box an press enter
2 - Click Create a New Profile

enter image description here

3 - Choose a name and folder for the new profile (take note of the profile location), in this case : d:ff_profilesselenium_user

enter image description here

4 - Activate the new browser profile

enter image description here

5 - Login to any website that you want to skip the login process on selenium, in this case,

6 - Once you've logged in successfully (after scanning the QR code) close the browser

7 - Using the profile on your script

from selenium import webdriver
fp = webdriver.FirefoxProfile('d:\ff_profiles\selenium_user') 
driver = webdriver.Firefox(firefox_profile=fp)
# you should still be logged in.
Tuesday, October 26, 2021
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :