使用python和硒的Web抓取eBay下拉文本

时间:2019-05-20 21:33:54

标签: python selenium selenium-webdriver

我正在尝试在ebay上输出所选下拉选项的文本。 我要输出文本,然后输出该项目的价格(最终),因为选择了不同的下拉选项(这就是为什么我不想一次全部刮取下拉列表的原因)。 我已经尝试过此代码:

from selenium import webdriver
import csv
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select

browser = webdriver.Chrome(executable_path='C:\Users\user\PycharmProjects\seleniumTest\drivers\chromedriver.exe')
browser.get('https://www.ebay.co.uk/itm/Wooden-Box-Coins-Coin-Capsules-Display-Storage-Case-for-Collectible-50-100-New/392274824564')

posts = browser.find_element_by_xpath("//select[@id='msku-sel-1']").send_keys(Keys.DOWN) // this just selects the option after select
for post in posts:
    print(post.text)

截屏: enter image description here

如果能提供一些帮助,将非常感谢!

但是,我在控制台中收到此错误。

C:\Python27\python.exe C:/Users/user/PycharmProjects/seleniumTest/test/test310.py
Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/seleniumTest/test/test310.py", line 18, in <module>
    for post in posts:
TypeError: 'NoneType' object is not iterable

2 个答案:

答案 0 :(得分:1)

您可以Select上硒课程。

from selenium.webdriver.support.select import Select

sel = Select(driver.find_element_by_xpath("//select[@id='msku-sel-1']"))

for index in range(1, len(sel.options)):
    # skipping index 0 because it is not valid option
    sel.select_by_index(index)
    print("{}: {}".format(sel.first_selected_option.text, browser.find_element_by_xpath("//span[@id='prcIsum']").text))

以上代码应提供如下输出:

S: £6.35
L: £10.25

答案 1 :(得分:0)

这是将单击每个选项并打印价格的逻辑。

options = driver.find_elements_by_xpath("//select[@id='msku-sel-1']/option")
for opt in range (len(options)):
    driver.find_element_by_xpath("(//select[@id='msku-sel-1']/option)[" +  str(opt+1) +  "]").click()
    print(driver.find_element_by_xpath("//span[@id='prcIsum']").text)