0

I'm Trying To Scrape Values in option tag with css selector but i can't:

I Want to scrape values attribute in option tag for example <option value='i want to scrape this'>text</option>

Here is another Screenshot so you can understand better:

enter image description here

in the option tag i want to scrape values not text

You Can See Here is the option values screenshot:

enter image description here

I Want also to scrape values here:

Here is my code:

cur = driver.find_elements_by_css_selector('#id_currency')
country = driver.find_elements_by_css_selector('search-form-place-country')
items = len(cur)

with open('cur.csv','w') as s:
    for i in range(items):
        s.write(cur[i].text + ',' + country[i].text + '\n')

Any Help Will Be Appreciated

Thanks!

ou_ryperd
  • 2,037
  • 2
  • 18
  • 23

3 Answers3

1

Use Select class, it is specifically for <select> dropdowns

from selenium.webdriver.support.select import Select

dropDown = driver.find_element_by_id('search-form-place-country')
select = Select(dropDown)
with open('cur.csv','w') as s:
    for option in select.options:
        s.write(option.get_attribute('value') + '\n')
SIM
  • 21,997
  • 5
  • 37
  • 109
Guy
  • 46,488
  • 10
  • 44
  • 88
0

just select them based on the option tag.

country= driver.find_elements_by_css_selector('option')

New for loop:

    for i in range(items):
        s.write(cur[i].text + ',' + country[i].get_attribute["value"] + '\n')

Bendik Knapstad
  • 1,409
  • 1
  • 9
  • 18
0

To extract the values with in the <option> tag with using you can use the following Locator Strategy:

from selenium import webdriver
from selenium.webdriver.support.ui import Select

select_places = Select(driver.find_element_by_css_selector("select.search-form-place.select.form-control"))
for option in select_places.options:
    print(option.get_attribute("value"))  
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Getting Error `Traceback (most recent call last): File "cur.py", line 11, in select_places = Select(driver.find_element_by_css_selector("select.search-fo rm-place.select.form-control")) NameError: name 'Select' is not defined` –  Nov 26 '19 at 09:48
  • @HamzaMirchi Added the imports, checkout the updated answer and let me know the status. – undetected Selenium Nov 26 '19 at 09:52