0

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

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.