Python Selenium Select Element From Drop Down. Element Not Visible Exception
I'm trying to scrape the following website link I need to automate the following steps: 1) Select the correct drop down table (the first on the left you see un the image below). 2)
Solution 1:
You can simply try click on the select element, then click on required option. It may help you.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
driver = webdriver.Chrome('path/to/the/driver.exe')
driver.get('https://www.costacrociere.it/B2C/I/Pages/Default.aspx')
driver.set_window_size(800, 660)
time.sleep(2)
select=driver.find_element_by_id("ctl00_ctl00_ctl00_ctl37_g_7e88f2a7_c220_4ba6_8ca8_49ca1297d22a_cruiseFinderControl_ddl_MacroArea")
select.click()
optionCaraibi=driver.find_element_by_xpath("//select[@id='ctl00_ctl00_ctl00_ctl37_g_7e88f2a7_c220_4ba6_8ca8_49ca1297d22a_cruiseFinderControl_ddl_MacroArea']/option[.='Caraibi']")
optionCaraibi.click()
Change the id of the above code if it is dynamic.
Solution 2:
The error says it all :
OUT: ElementNotVisibleException: element not visible: Element is nocurrently visible and may not be manipulated(Session info: crome=63.0.3239.132)
The error clearly indicates that the element with which you are trying to interact is not visible as they are dynamic. So we have to induce Explicit Wait
for the element_to_be_visible as follows :
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://www.costacrociere.it/B2C/I/Pages/Default.aspx')
driver.maximize_window()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='selectric-wrapper selectric-ddlMacroArea']"))).click()
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='selectric-items']//ul//li[text()='Caraibi']"))).click()
print("Option Caraibi clicked")
Console Output :
Option Caraibi clicked
Post a Comment for "Python Selenium Select Element From Drop Down. Element Not Visible Exception"