2

I want to click on one of the options in one of these bad boys: https://gyazo.com/86a2f8773b182e730fe5c69107efa190 How can I make my code click it and choose the option with the value="196277"?

<select name="Input.MatrixElements[0].ValueId" id="matrix-element-666" data-val-required="Produkt skal udfyldes" data-val="true" data-listingtext-format=", {0}" data-listingtext-desc="" data-listingtext-priority="1" data-element-desc="Produkt" class="field-medium" data-matrixid="666">
            <option value="">- Vælg -</option>
                    <option value="115604" data-listingtext-desc="Børneur">Børneur</option>
            <option value="17662" data-listingtext-desc="Dameur">Dameur</option>
            <option value="17663" data-listingtext-desc="Dykkerur">Dykkerur</option>
            <option value="17661" data-listingtext-desc="Herreur">Herreur</option>
            <option value="17665" data-listingtext-desc="Lommeur">Lommeur</option>
            <option value="245187" data-listingtext-desc="Smartwatch">Smartwatch</option>
            <option value="103440" data-listingtext-desc="Smykkeur">Smykkeur</option>
            <option value="171750" data-listingtext-desc="Stopur">Stopur</option>
            <option value="196277" data-listingtext-desc="Unisexur">Unisexur</option>
            <option value="23395" data-listingtext-desc="Andet">Andet</option>
    </select>
2
  • Welcome to Stack Overflow! Please read How to Ask. Please provide the code you have tried and the execution result including any error messages, etc. Commented Nov 13, 2016 at 2:17
  • Did you get a valid answer ? Commented Dec 2, 2021 at 12:00

3 Answers 3

1

You can use xpath to find the particular option element :

your_choice=browser.find_element_by_xpath("//select/option[@value='196277']")

Then call the click() function on it :

your_choice.click()
Sign up to request clarification or add additional context in comments.

5 Comments

I get this error msg, when trying that: gyazo.com/179e4a5240bb058f54b1cb5d48d8c16c
The xpath must be incorrect then, since there are other html elements surrounding the select tag too, i presume. This page - wikihow.com/Find-XPath-Using-Firebug , will help you find the particular xpath for the option element on your page. After that, the click function will work as desired.
I use chrome .. Not firefox
This actually worked, I just had to put in a sleep(2) because the reason why it didnt work was that it search for the //select/option[@value='196277'] before even finnished loading. Thats why it gave me the error that it couldnt find it. It didnt finnish loading first! before searching.
Somehow this does not work on FireFox webdriver, but thats not the question here. ^^
0

This could work for you:

from selenium.webdriver.support.ui import Select
from selenium import webdriver as driver
menu = Select(driver.find_element_by_id('matrix-element-666'))
for option in menu:
    if option.value == '196277':
        option.select
    else:
        pass

6 Comments

line 2 - driver not defined
That will be your webdriver from selenium
Doesnt work either, the error I get: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"class name","selector":"field-medium"}
The gives us more details. We can't help if we don't know you code. That site might have a iframe, or frames. That must be handled first, wich is not part of the question.
What do you want the full html?
|
0

Apperantly none of the methods i found on stackoverflow or in the documentation worked (in FireFox webdriver only ^^°). So i made this workaround which makes a transition between the value of the option and the text of the option. The options text will then be sent as keys to select the right option in the browser, lol!

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
import time #debugging using your eyes
from captchaUrlToText import CaptchaUrlToText
import getopt
import sys
import tracebackclass 

StackSelectOptionScrape(unittest.TestCase):

def setUp(self):
    #self.driver = webdriver.Firefox()
    self.driver = webdriver.Chrome()
    self.driver.implicitly_wait(30)
    url ='http://localhost/pysel.html'#location with select/option code
    self.base_url = url
    self.verificationErrors = []
    self.accept_next_alert = True

def test_yadda(self):
    driver = self.driver
    driver.get(self.base_url)
    self.selectOptionByValue('//select','196277',driver)

def selectOptionByValue(self,selectElem,optionValue,driver):
    """Select an option of a drop down menu using selenium webdriver Chrome or FireFox

    selecting the text that belongs to a value will always work since the options
    always have a unique value and text, otherwise they would not make much sense as
    options, would they? =) """

    # first get the keys to send
    keys = ""
    element = driver.find_element_by_xpath(selectElem)
    all_options = element.find_elements_by_tag_name("option")
    for option in all_options:
       value = option.get_attribute("value")
       text = option.get_attribute("text")
       print("Value is: %s" % value)
       print("Text is: %s" % text)
       if value == optionValue:
           keys = text

    # now send keys if menu is popped up
    element.click() # make menu pop up
    element.send_keys(keys) # send keys to select option (text)
    element.click() # select option
    time.sleep(3) # verify with your eyes ^^-d


def is_element_present(self, how, what):
    try: self.driver.find_element(by=how, value=what)
    except NoSuchElementException, e: return False
    return True

def is_alert_present(self):
    try: self.driver.switch_to_alert()
    except NoAlertPresentException, e: return False
    return True

def close_alert_and_get_its_text(self):
    try:
        alert = self.driver.switch_to_alert()
        alert_text = alert.text
        if self.accept_next_alert:
            alert.accept()
        else:
            alert.dismiss()
        return alert_text
    finally: self.accept_next_alert = True

def tearDown(self):
    self.driver.quit()
    self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

I dont know, sometimes selenium is like hell, so if you are looking for a good scraping (not webtesting) framework i recommend scrapy.org !

2 Comments

what is self equal to?
self is a python thingy, see here stackoverflow.com/questions/2709821/…. self.driver is the webdriver

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.