如何使用selenium

时间:2016-03-25 09:08:58

标签: javascript python html selenium

我尝试通过CSS选择器选择页面上的下拉菜单(没有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>

我尝试使用以下代码选择每个下拉元素,之后我想查找相关的视频数据(注意,我假设我不知道下拉列表中的内容是什么,因为我想要它适用于本网站上的任何下拉菜单):

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部分来自以下stackoverflow discussion(第二个答案)。但是,我收到以下错误:

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

3 个答案:

答案 0 :(得分:2)

也许你在python中使用java methond。 以下是Java方法:

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

以下是Python方法:

droplist = driver.find_element_by_css_selector('select')

答案 1 :(得分:1)

我会使用项目的值来找到下拉列表。以下是您的网页的一个工作示例:

var lastScrollTop = 0;
$(window).scroll(function(event){
    var st = $(this).scrollTop();
        if (st > lastScrollTop){
            // downscroll code
        } else {
           // upscroll code
    }
    lastScrollTop = st;
});

请注意第二项&#34; PlaceHolder&#34;没有联系。

答案 2 :(得分:0)

您出租车尝试使用onchange属性

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

这将为您提供具有onchange属性的元素,其中包含"document.getElementById('11qq')"