I'm having trouble pulling information from a table using Selenium. The HTML looks something like this:
<select id="software_version" name="software_version" onchange="CheckDropDownValueUIUpdate('software_version', 'software_version_image','software_version_message', 'Software Version');" style="visibility: visible;">
<option value="none">
Select a Version</option>
<option value="1732732">
12</option>
<option value="456456">
14.0.69</option>
<option value="68767">
13.62</option>
<option value="678934">
22.0.33</option>
</select>
Right clicking and selecting X-Path on Chrome for the 14.0.69 would return:
//*[@id="software_version"]/option[3]
So I put it in my code:
try:
query = driver.find_element("xpath", '//*[@id="software_version"]/option[3]')
SoftwareVersion = query.text
print(SoftwareVersion)
except NoSuchElementException:
new_row = {'Software': dfSoftwareName, 'Version': "Not Available"}
This yielded no result.
So I did a little reading and learned about selectByIndex and tried this:
try:
query = driver.find_element("xpath", '//*[@id="software_version"]')
query.selectByIndex(3);
SoftwareVersion = query.text
print(SoftwareVersion)
except NoSuchElementException:
new_row = {'Software': dfSoftwareName, 'Version': "Not Available"}
But this yielded the result "AttributeError: 'WebElement' object has no attribute 'selectByIndex'". At this point I don't know where to look since the first option gave me no feedback. Thank you for any suggestions you can provide.
CodePudding user response:
You need to import the following:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
And then you can locate an option like this:
options = WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, "//select[@id='software_version']/option")))
You can print the full list of options:
print('options:', [x.text for x in options])
Or you can print an individual one:
print('Third from the list:', options[2].text)
You can filter them by text:
the_one_youre_searching = [x for x in options if '14.0.69' in x.text][0]
You can get their attributes:
value_attribs = [x.get_attribute("value") for x in options]
Selenium documentation can be found at https://www.selenium.dev/documentation/