2016-03-25 9 views
0

CSSセレクタでページを選択してページにドロップダウンメニュー(idがない)を選択しようとしましたが、動作させることができません。ここでは、ドロップダウン・コードは次のとおりです。セレクションを使用してIDなしのドロップダウンメニューを選択する方法

<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> 

私は私が何であるかを知らないことを仮定している、私は注意してください(該当する映像データを検索したい後、各ドロップダウン要素を選択するには、次のコードを使用してみました私はそれがこのサイト上のすべてのドロップダウンのために働くしたいのですから、そもそもドロップダウン):私は、次のstackoverflow discussion(第二の答え)から得た

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 

Select droplist一部。しかし、私は次のエラーを取得する:

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

答えて

2

多分あなたはpythonでjavaのmethondを使用します。次 は、Javaメソッドである:

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

次のPythonのメソッドです:

droplist = driver.find_element_by_css_selector('select') 
+0

'droplist.selectByVisibleText(x)'も有効ではないようですが、これと同じPythonは何ですか? –

+0

'Florent、B'は、 – gavinsun

+0

と言っています。 'Florent、B'は次のような方法で参照することができます: 'Select(driver.find_element_by_xpath(" // select [option = '"+ x +"'] ") ).select_by_visible_text(x) 'は それにはPythonのAPI以下: [PythonのAPIを](http://seleniumhq.github.io/selenium/docs/api/py/api.html) 私はそれを願っていますあなたを助けられる。 [select_by_visible_text](http://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.select.html#selenium.webdriver.support.select.Select.select_by_visible_text) – gavinsun

0

あなたのタクシーは、これはあなたの要素の魔女を与えるonchange属性

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

を使用しようonchange属性の魔女が"document.getElementById('11qq')"が含まれています。

1

私はドロップダウン・リストを検索する項目の値を使用します。 2番目の項目「プレースホルダ」何のリンクを持っていないことを

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() 

注:ここではあなたのページと実施例です。

+0

Hmmm、ビデオの存在をチェックし、ビデオが存在しない場合は、次の 'x 'ループで?残念なことに、手動で 'x'の値を入力することはできません。 –

+0

ビデオの存在を確認するには、値が "http"で始まる "オプション"要素をループするだけです。手動でxを入力する必要はありませんが、これはあなたのケースでselectの使い方を示す例に過ぎませんでした。 –

関連する問題