-1

I am doing webscraping using selenium web driver. I have done with an option value but I have to do with option text. I have to scrape an option text and pass to your_choice=driver.find_element_by_xpath("//select/option[@value = {}]".format(b)). Below is the code

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from bs4 import BeautifulSoup

driver = webdriver.Firefox(executable_path='./geckodriver')
url = 'https://ipr.etsi.org'
driver.get(url)
button = driver.find_element_by_id(("btnConfirm"))
button.click()
select_element = Select(driver.find_element_by_name("ctl00$cphMain$lstCompany"))
data = []
for option in select_element.options:
    data.append(option.get_attribute('value'))
for a in data:
    b = a
    your_choice = driver.find_element_by_xpath("//select/option[@value = {}]".format(b))
    # continue
    your_choice.click()
python_button = driver.find_element_by_id(("ctl00_cphMain_btnSearch"))
python_button.click()

Above is the code I have done with value of the option. Now I have to do it with option text.

Abdul Raoof
  • 133
  • 7
  • Ignore the selected answer... that's really old. Instead use [this answer](https://stackoverflow.com/a/28613320/2386774). – JeffC May 17 '19 at 19:32
  • Also, any time you have a question, you should do some googling and reading first. This question has been asked many times on this site and others along with blogs, articles, etc. You can see in the link above that the question was asked in 2011. – JeffC May 17 '19 at 19:33

2 Answers2

2

since you are looking at selecting by text.

Changing option.get_attribute('value') to

    data.append(option.text) #this will add 'Optis Cellular Technology','Orange',etc to data

and "//select/option[@value = {}]" to

    if a != '':   #to skip the first option because it's empty
      your_choice = driver.find_element_by_xpath("//select/option[text()='{0}']".format(a))
      your_choice.click() 
Linda
  • 627
  • 4
  • 14
  • 1
    This answer would be better if you added a short explanation. code-only answers can be hard to understand, since it's not clear if the solution lies in the first line, the last line, somewhere in between, or if every line is required. – Bryan Oakley May 15 '19 at 20:02
1

If you want to select by visible text, python has the select_by_visible_text method. All you need to do is to capture the option element like this:

select = Select(driver.find_element_by_id("option_id"))

Or via any other selector of your choice, then just use the function:

select.select_by_visible_text("visible_text")
Moro
  • 781
  • 4
  • 14