I should use this:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import Select
with open("sel_path.txt") as path:
s = path.read()
serv = Service(s)
driver = webdriver.Chrome(service=serv)
driver.get("https://pynishant.github.io/dropdown-selenium-python-select.html")
lang = driver.find_element(By.ID, "lang1")
select = Select(lang)
select.select_by_value("1")
But I get this error:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: Element is not currently visible and may not be manipulated
I think its not working since the elements show up after the usr clicks the box but I have no idea how to get around that to select the element with Selenium
Ultimately I am trying to target this box.
I don't know if its because of the old Selenium syntax getting deprecated or not...
Any help would be greatly appreciated.
CodePudding user response:
Cause the dropdown HTML is:
<div >
<div>PHP</div>
<div>Python</div>
<div>Java</div>
<div>C#</div>
</div>
So you can not use select.
Instead, use:
serv = Service(s)
driver = webdriver.Chrome(service=serv)
wait = WebDriverWait(driver, 30)
driver.maximize_window()
driver.get("https://pynishant.github.io/dropdown-selenium-python-select.html")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.select-selected"))).click()
drop_down_list = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "div.select-items div")))
desired_option_to_select = 'PHP'
for option in drop_down_list:
if option.text == desired_option_to_select:
option.click()
print('Clicked ', desired_option_to_select, 'option')
break
else:
print('Could not click')
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
CodePudding user response:
You were close enough. However a few points:
Test Lang section
- It's not a SELECT tag so you can't use
Select()
Solution
To select PHP from the dropdown you can use the following Locator Strategies:
Code Block:
driver.get("https://pynishant.github.io/dropdown-selenium-python-select.html")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='select-selected']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='select-items']/div[text()='PHP']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
Select your lang section
The SELECT tag have the
id
attribute as lang but not lang1. So the effective line of code will be:lang = driver.find_element(By.ID, "lang")
There is no
option
with value as 1 but php, c#, python and java
Solution
You can use the following Locator Strategies:
Code Block:
driver.get("https://pynishant.github.io/dropdown-selenium-python-select.html")
Select(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//select[@id='lang']")))).select_by_value("php")
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
Browser Snapshot: