codeisfun1234
codeisfun1234

Reputation: 11

Web scraping ebay dropdown text with python and selenium

I am trying to output the text of a the selected dropdown option on ebay. I want to output the text and then the price of the item (eventually) as different drop down options are selected (which is why i don't want to scrape a list of dropdown values all at once). I have tried this code:

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)

Screenshot: enter image description here

Would be extremely grateful if some help can be provided!

However, I received this error in the console.

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

Upvotes: 0

Views: 330

Answers (2)

Kamal
Kamal

Reputation: 2554

You can you Select class in selenium.

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))

Above code should give output like:

S: £6.35
L: £10.25

Upvotes: 1

supputuri
supputuri

Reputation: 14145

Here is the logic which will click on each of the option and print the price.

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)

Upvotes: 0

Related Questions