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!

4
  • What values you want to get? Commented Nov 26, 2019 at 9:32
  • in the option tag i wan't to scrap values attribute Commented Nov 26, 2019 at 9:32
  • You want to fetch Country name? Commented Nov 26, 2019 at 9:37
  • Question Updated! Commented Nov 26, 2019 at 9:40

3 Answers 3

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

Comments

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

2 Comments

Getting Error Traceback (most recent call last): File "cur.py", line 11, in <module> select_places = Select(driver.find_element_by_css_selector("select.search-fo rm-place.select.form-control")) NameError: name 'Select' is not defined
@HamzaMirchi Added the imports, checkout the updated answer and let me know the status.
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')

4 Comments

no i don't want whole option tag i want option tag and inside there i want to scrap values attribute
I Want to scrap values attribute in option tag for example <option value='i want to scrap this'>text</option>
then just replace the .text part in your for loop with .getAttribute("value")
Sorry the syntax is get_attribute() not getAttribute()

Your Answer

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