user4962385
user4962385

Reputation:

How to select dropdown menu without id using selenium

I've tried selecting a dropdown menu (that has no id) on a page by selecting it by CSS selector, but I can't get it to work. Here is the dropdown code:

<select style="margin: 5px auto; width: 146px;" onchange="document.getElementById('11qq').src=this.options[this.selectedIndex].value;">
<option value="https://player.vimeo.com/video/158733095">Shakedown</option>
<option value="x">Placeholder</option>
<option value="https://player.vimeo.com/video/158815551">Race</option>
</select>

I tried using the following code to select each dropdown element, after which I want to find relevant video data (Note, I'm assuming that I do not know what is in the dropdown to begin with, since I'd like it to work for any dropdown on this site):

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
import urllib2

url = "http://racing4everyone.eu/2016/03/12/formula-e-201516-round05-mexico/"
page = urllib2.urlopen(url)
soup = BeautifulSoup(page.read(), "html.parser")

dropdown = [x.text for x in soup.find_all('option')]

driver = webdriver.Firefox()
driver.get("http://racing4everyone.eu/2016/03/12/formula-e-201516-round05-mexico/")

for x in dropdown:
    Select droplist = new Select(driver.findElement(By.CSS_SELECTOR("select")));
    droplist.selectByVisibleText(x);

    frame_video = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "iframe[src*=video]")))
    driver.switch_to.frame(frame_video)
    wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".controls")))
    page_source = driver.page_source
    driver.close()

    soup = BeautifulSoup(page_source, "html.parser")
    script = soup.find_all("script")
    # A couple of other operations follow to isolate the relevant data from the script data

The Select droplist part I got from the following stackoverflow discussion (Second answer). However, I get the following error:

Select droplist = new Select(driver.findElement(By.CSS_SELECTOR("select")));
                  ^
SyntaxError: invalid syntax

Upvotes: 0

Views: 3277

Answers (3)

Florent B.
Florent B.

Reputation: 42528

I would use the value of an item to locate the drop down list. Here is a working example with your page:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select

driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)

driver.get("http://racing4everyone.eu/2016/03/12/formula-e-201516-round05-mexico/")

for x in ["Shakedown", "Race"]:
  # select the option
  Select(driver.find_element_by_xpath("//select[option='" + x + "']")).select_by_visible_text(x)

  # set context on the video frame
  frame_video = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "iframe[src*=video]")))
  driver.switch_to.frame(frame_video)

  # set the default context
  driver.switch_to_default_content()

driver.quit()

Note that the second item "PlaceHolder" has no link.

Upvotes: 0

gavinsun
gavinsun

Reputation: 36

maybe you use the java methond in python. The following is the Java method:

Select droplist = new Select(driver.findElement(By.CSS_SELECTOR("select")));

The following is the Python method:

droplist = driver.find_element_by_css_selector('select')

Upvotes: 2

Guy
Guy

Reputation: 50864

You cab try to use the onchange attribute

driver.find_element_by_css_selector('onchange*="document.getElementById('11qq')"')

This will give you the element witch has onchange attribute witch contains "document.getElementById('11qq')".

Upvotes: 0

Related Questions