繁体   English   中英

使用Selenium Python加载页面后,如何保存特定页面?

[英]How can I hold a specific page after the page is loaded using Selenium Python?

我为此页面制作了一个网络爬虫( http://www.bobaedream.co.kr/cyber/Cyber​​Car.php?gubun=I )来收集每一页的库存清单。 首先,我的代码从操纵“搜索菜单”部分中的下拉菜单开始,但是在迭代过程中页面加载和保持存在一些问题。 我想做的是加载页面并保持该页面,直到该页面的爬网操作完成为止。

下面是我的代码:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import WebDriverException
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from urllib import parse
from time import sleep

self.link = 'http://www.bobaedream.co.kr/cyber/CyberCar.php?gubun=I'
self.driver = webdriver.PhantomJS()
self.driver.set_window_size(1920, 1080)
self.driver.get(self.link)
self.wait = WebDriverWait(self.driver, 10)

def option2_menu_loaded(inDriver):
        path = '//select[@id="level2_no"]'
        return inDriver.find_element_by_xpath(path)

self.wait.until(option2_menu_loaded)

while True:
    try:
        select_option2_values = [
            ('%s' % o.get_attribute('text'), '%s' % o.get_attribute('value'))
            for o
            in Select(self.driver.find_element_by_css_selector("#level2_no")).options
            if o.get_attribute('text') != '세부등급']
    except (StaleElementReferenceException, NoSuchElementException):
        print("=======Exception Found - Option2 Save=====")
        self.driver.refresh()
        self.driver.implicitly_wait(1.5)
        continue
    break

for option2 in select_option2_values:
    self.csv.setCarTitle(ma, mo, de, option1[0], option2[0])

    print(option2[0], option2[1])
    self.driver.implicitly_wait(0.5)

    while True:
        try:
            Select(self.driver.find_element_by_css_selector("#level2_no")).select_by_value(option2[1])

        except (StaleElementReferenceException, NoSuchElementException):
            print("=======Exception Found - Option2 Request=====")
            self.driver.refresh()
            self.driver.implicitly_wait(1.5)
            self.driver.refresh()
            continue
        break

我猜第五行之后的某些类型的“ self.wait.until(EC。〜)”代码“ self.wait.until(option2_menu_loaded)”可能会有所帮助。 我尝试了很多,但是找不到任何解决方案。

请帮我解决这个问题。

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID,"level2_no")))

如果要在该元素上执行任何操作之前等待该元素出现,则可能需要它。 在代码开头处from selenium.webdriver.common.by import By - from selenium.webdriver.common.by import By

仅供参考,我注意到select标记仅包含一个选项-세부등급。 因此,除非您期望#level2_no将来包含更多元素,否则select_option2_values数组将为空。

<select id="level2_no" name="level2_no" onmousedown="$('.maker').hide();" onchange="car_depth_step_new(this.value, 4);" style="width:112px" title="세부등급 선택">
<option value="">세부등급</option>
</select>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM