如何使用两个不同的下拉列表进行网络爬行

问题描述 投票:0回答:1
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
import csv
import time

driver = webdriver.Chrome('./chromedriver')


driver.get('https://price.joinsland.joins.com/theme/index_theme.asp?sisaegbn=T05')

element = driver.find_element_by_xpath('//select[@name="sido"]')
options = element.find_elements_by_tag_name("option")

for option in options:
    option.click()
    time.sleep(5)

    element2 = driver.find_element_by_xpath('//select[@name="gugun"]')
    options2= element2.find_elements_by_tag_name("option")

    for each in options2:
        each.click()
        time.sleep(4) 

我已经尝试过此代码,但是出现错误提示:

“消息:陈旧元素引用:元素未附加到页面文档中”

enter image description here

python selenium dropdown
1个回答
0
投票
这就是我想出的。但是我知道代码是否正确。我不懂python。

//get select select1 = Select(driver.find_element_by_xpath('//select[@name="sido"]')) //get all options from select options1 = select1.options for opt1 is options1: //select the option which has the value of opt1 select1.select_by_value(opt1.get_attribute("value")) time.sleep(5) select2 = Select(driver.find_element_by_xpath('//select[@name="gugu"]')) options2 = select2.options for opt2 in options2: select1.select_by_value(opt2.get_attribute("value")) time.sleep(4)

© www.soinside.com 2019 - 2024. All rights reserved.