How To Scrap Values in Selenium Using Python?

好久不见. 提交于 2020-05-07 09:23:42

问题


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

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

Here is another Screenshot so you can understand better:

in the option tag i want to scrap values not text

You Can See Here is the option values screenshot:

I Want also to scrap 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!


回答1:


To extract the values with in the <option> tag with using css-selectors 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:


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



回答3:


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



来源:https://stackoverflow.com/questions/59047721/how-to-scrap-values-in-selenium-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!