【问题标题】:Click on dropdown menu with selenium, scrape doesn't work单击带有硒的下拉菜单,刮擦不起作用
【发布时间】:2015-07-17 12:51:03
【问题描述】:

我想从http://www.squawka.com/match-results 抓取数据。首先我想在下拉菜单中选择联赛,例如美洲,德甲。我的代码选择了美洲,但未加载新数据。我认为后台有一些 javascript 没有启动。

这里是代码。我将scrapy和selenium与chromedirver一起使用,但我也测试了firefox驱动程序但没有成功。

import scrapy

from squawka.items import SquawkaItem
from scrapy.http import FormRequest, Request

from selenium import selenium
from selenium import webdriver
import time

class SquawkaSpider(scrapy.Spider):
    name = "squawka"
    allowed_domains = ["squawka.com"]
    start_urls = ["http://www.squawka.com/match-results"]

def __init__(self):
    self.driver = webdriver.Chrome(executable_path='/Users/fabian/chromedriver')

def parse(self, response):
    self.driver.get(response.url)
    time.sleep(5)
    Dropdown = self.driver.find_element_by_xpath("//*[@id='league-filter-list']/option[contains(text(), 'The Americas')]").click()

我希望有人可以帮助我。

谢谢

【问题讨论】:

  • 我什至不能手动选择“美洲”..
  • 真的吗?那很奇怪。手动我可以在我的网络浏览器中选择每个联赛,例如美洲或德甲。
  • 网站运行正常。这不是最终解决方案,但请阅读有关隐式/显式等待以摆脱睡眠的内容! (docs.seleniumhq.org/docs/…)。例如,您可以直到元素 .//*[@id='league-filter-list'](下拉菜单)可用。
  • @metar 你的意思是这样的吗:wait = WebDriverWait(self.driver, 10) 然后在下一行 element = wait.until(EC.element_to_be_clickable((By.ID,'league -过滤器列表')))。代码正在运行。这样对吗?我的刮刀是否等到下拉菜单可点击?

标签: javascript python selenium web-scraping scrapy


【解决方案1】:

您的问题可以通过等待时间和 Select() 语句的组合来解决。 在这种情况下,您需要放置一些等待时间。隐式/显式等待都可以在这里工作。我尝试过隐式等待

我已尝试使用以下代码,它正在从下拉列表中选择“美洲”。

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class ChromeDriver {

    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("http://www.squawka.com/match-results");
        System.out.println("Entered Url");
        WebElement element1=driver.findElement(By.xpath("//*[@id='league-filter-list']"));
        Select sel = new Select(element1);
        sel.selectByVisibleText("The Americas");
        System.out.println("The Americans is selected");
    }
}

注意:- 应用程序的设计方式是,您从下拉列表中选择任何内容,然后它会再次切换回“欧洲前 5 名联赛”。但我可以选择“美洲”上面的代码。

【讨论】:

  • 噗,好吧,现在我有点困惑。那是python代码吗?并请注意。你说你可以选择“美洲”,然后它又回到了欧洲联赛的前 5 名。我的代码相同,但问题是主页没有加载联盟的数据。如果我选择德甲联赛,则不会有德甲联赛的数据,但会加载其他内容。
  • 我是手动完成的,从下拉菜单中选择了“德甲”,但它仍然会加载其他数据。
  • 如果我使用自己的浏览器并手动选择“德甲”,则效果很好。但在 Scraper 中却没有。
【解决方案2】:
Because you are not switching to the frame .You have to switch to that Frame before selecting the value.

driver.switchTo().frame(name_or_id);

现在从下拉列表中选择值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    相关资源
    最近更新 更多