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>
Sebastian Nielsen
  • 3,835
  • 5
  • 27
  • 43

3 Answers3

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()
Anas Khan
  • 98
  • 2
  • 9
  • I get this error msg, when trying that: https://gyazo.com/179e4a5240bb058f54b1cb5d48d8c16c – Sebastian Nielsen Nov 12 '16 at 22:01
  • The xpath must be incorrect then, since there are other html elements surrounding the select tag too, i presume. This page - http://www.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. – Anas Khan Nov 12 '16 at 22:17
  • I use chrome .. Not firefox – Sebastian Nielsen Nov 12 '16 at 22:22
  • 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. – Sebastian Nielsen Nov 13 '16 at 09:47
  • Somehow this does not work on FireFox webdriver, but thats not the question here. ^^ – Lord_PedantenStein Nov 13 '16 at 22:35
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
dmb
  • 288
  • 2
  • 9
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 !