Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
选择dependent下拉列表并单击Python中的选项时出错_Python_Selenium_Web Scraping_Beautifulsoup - Fatal编程技术网

选择dependent下拉列表并单击Python中的选项时出错

选择dependent下拉列表并单击Python中的选项时出错,python,selenium,web-scraping,beautifulsoup,Python,Selenium,Web Scraping,Beautifulsoup,我试图从所有下拉元素的组合中提取链接。我有下面的代码,但当我运行代码时,它会抛出错误 url = "https://www.ford.co.uk/owner/my-vehicle/download-your-manual" from selenium import webdriver from selenium.webdriver.support.ui import Select import time driver = webdriver.Chrome('C:/Users/chromedr

我试图从所有下拉元素的组合中提取链接。我有下面的代码,但当我运行代码时,它会抛出错误

url = "https://www.ford.co.uk/owner/my-vehicle/download-your-manual"

from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time

driver = webdriver.Chrome('C:/Users/chromedriver.exe')
driver.get(url)
time.sleep(4)
selectYear = Select(driver.find_element_by_id("odl-selected-year"))

data = []
for yearOption in selectYear.options:
    yearText = yearOption.text
    selectYear.select_by_visible_text(yearText)
    time.sleep(1)

    selectModel = Select(driver.find_element_by_id("odl-selected-model"))
    for modelOption in selectModel.options:
        modelText = modelOption.text
        selectModel.select_by_visible_text(modelText)
        data.append([yearText,modelText])


button = driver.find_element_by_link_text("Select this vehicle")
button.click()
page = driver.page_source
soup = BeautifulSoup(page, 'html.parser')
content = soup.findAll('a',attrs={"class":"odl-download-link"})

links =[]
for i in content:
    links.append(i["href"])

我不确定我会错在哪里。先谢谢你。

首先:我必须补充

from bs4 import BeautifulSoup
第二:我必须改变缩进

第三:我必须增加时间。睡眠(2)在获得链接页面之前


最后:我点击
总是把完整的错误信息(从单词“Traceback”开始)作为文本(不是截图)放在问题中(不是评论)。还有其他有用的信息。当我从bs4 import BeautifulSoup添加
时,我更改了我的问题,然后代码对我有效,没有错误。要获取所有组合的页面,您必须选择第一个组合,单击按钮,从新页面获取链接,使用
驱动程序再次加载上一页。获取(url)
选择第二个组合A并单击按钮,从第二个页面获取链接并再次加载上一个页面,等等。如果列表为空,您可以单击链接
链接列表。这是错误“消息:元素单击被拦截:元素在点(588665)处不可单击”。其他元素将收到点击:…

(会话信息:chrome=80.0.3987.116)“在代码上方运行有时页面会显示弹出窗口,并且选择被隐藏-代码需要检查是否存在弹出窗口,然后在此窗口上点击
关闭
url = "https://www.ford.co.uk/owner/my-vehicle/download-your-manual"

from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
from bs4 import BeautifulSoup

driver = webdriver.Chrome() #'C:/Users/chromedriver.exe')
driver.get(url)
time.sleep(4)
selectYear = Select(driver.find_element_by_id("odl-selected-year"))

data = []
for yearOption in selectYear.options:
    yearText = yearOption.text
    selectYear.select_by_visible_text(yearText)
    time.sleep(1)

    selectModel = Select(driver.find_element_by_id("odl-selected-model"))
    for modelOption in selectModel.options:
        modelText = modelOption.text
        selectModel.select_by_visible_text(modelText)
        if yearText != 'Year' and modelText != 'Model':
            data.append([yearText, modelText])

            #print(data)

            button = driver.find_element_by_link_text("Select this vehicle")
            button.click()
            time.sleep(2)

            page = driver.page_source
            soup = BeautifulSoup(page, 'html.parser')
            content = soup.findAll('a', attrs={"class": "odl-download-link"})

            links =[]
            for i in content:
                links.append(i["href"])

            print(links)

            # go back

            driver.find_element_by_class_name('odl-back-link').click()