0

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) Select an option from the drop down menu (Caraibi option).

3) Click on the search button.

Drop down images: The first on the left ("Dove vuoi andare?"). enter image description here

The HTML code is the following one:

 <select name="ctl00$ctl00$ctl00$ctl37$g_7e88f2a7_c220_4ba6_8ca8_49ca1297d22a$cruiseFinderControl$ddl_MacroArea" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ctl00$ctl00$ctl37$g_7e88f2a7_c220_4ba6_8ca8_49ca1297d22a$cruiseFinderControl$ddl_MacroArea\',\'\')', 0)" id="ctl00_ctl00_ctl00_ctl37_g_7e88f2a7_c220_4ba6_8ca8_49ca1297d22a_cruiseFinderControl_ddl_MacroArea" class="ddlMacroArea" tabindex="0">
        <option selected="selected" value="">Tutte le destinazioni</option>
        <option value="NORTHERN CAPITALS">Capitali Nordiche</option>
        <option value="EASTERN CARIBBEAN">Caraibi</option>
        <option value="MAR ROSSOARAB">Dubai/Emirati Arabi</option>
        <option value="NORWEGIAN FJORDS">Fiordi, Spitzbergen e Islanda</option>
        <option value="PACIFIC OCEAN">Giro del Mondo</option>
        <option value="WEST MEDITERRANEAN">Mediterraneo Occidentale</option>
        <option value="EAST MEDITERRANEAN">Mediterraneo Orientale</option>
        <option value="ATLANTIC OCEAN">Oceano Atlantico/Canarie</option>
        <option value="INDIAN OCEAN">Oceano Indiano, Maldive, Mauritius</option>
        <option value="ORIENTAL LANDS">Oriente</option>
        <option value="SOUTH AMERICA">Sud America</option>
        <option value="TRANSATLANTIC">Transatlantiche</option>

    </select>

Well I'm using this code:

 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=Select(driver.find_element_by_id("ctl00_ctl00_ctl00_ctl37_g_7e88f2a7_c220_4ba6_8ca8_49ca1297d22a_cruiseFinderControl_ddl_MacroArea"))


 #view of the grappled options
 select.options

So until here i can get all the options (this is a part of them):

 [<selenium.webdriver.remote.webelement.WebElement 
 (session="7978296e5858040f56f27f3414087c60", element="0.7352996272394383-2")>, 
 <selenium.webdriver.remote.webelement.WebElement 
 (session="7978296e5858040f56f27f3414087c60", element="0.7352996272394383-3")>, 
 <selenium.webdriver.remote.webelement.WebElement 
 (session="7978296e5858040f56f27f3414087c60", element="0.7352996272394383-4")> 

So when I try to select the option by visible text, for example 'Caraibi' I get the following error:

# select by visible text
select.select_by_visible_text('Caraibi') 


OUT: ElementNotVisibleException: element not visible: Element is nocurrently 
visible and may not be manipulated(Session info: chrome=63.0.3239.132)
(Driver info: chromedriver=2.33.506120 
(e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 6.1.7601 SP1 
x86_64)

Thanks for your help!!!

CosimoCD
  • 3,570
  • 3
  • 22
  • 31
  • Possible duplicate of [Selecting a value from a drop-down option using selenium python](https://stackoverflow.com/questions/22524621/selecting-a-value-from-a-drop-down-option-using-selenium-python) – L_Church Jan 19 '18 at 09:27
  • Please read why a [**`screenshot of HTML or code or error is a bad idea`**](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Consider updating the Question with formatted text based HTML and code trials. – undetected Selenium Jan 19 '18 at 09:30
  • @L_Church I have already tried that code but it doesn't works. – CosimoCD Jan 19 '18 at 10:07
  • 2
    The ` – Florent B. Jan 19 '18 at 10:29
  • Your question is **unclear**. You haven't mentioned which dropdown you are referring by `Select the correct drop down table.` – undetected Selenium Jan 19 '18 at 11:25
  • @DebanjanB I have updated the question with the image of the drop down table. – CosimoCD Jan 19 '18 at 11:35

2 Answers2

0

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
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Try out my updated `Answer`. Moreover `it doesn't work` doesn't throws any light whats wrong happening. Update the question with your recent code trial and error stack trace. – undetected Selenium Jan 19 '18 at 10:51
  • I have tried your updated answer but it still doesn't work, i get this error: File "C:\ProgramData\Anaconda2\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) TimeoutException – CosimoCD Jan 19 '18 at 11:01
  • Checkout my updated answer and let me know the status. – undetected Selenium Jan 19 '18 at 11:56
  • you are really patient, I really apreciate it! I have tried your last update but it doesn't work. Same error as above: TimeoutException(message, screen, stacktrace) TimeoutException – CosimoCD Jan 19 '18 at 12:38
  • Provided the full code, ensure you are on latest Selenium 3.8.1, ChromeDriver 2.35 and Chrome 64.x. In production use `ChromeOptions` class to maximize the view instead of `driver.maximize_window()`. I can share the `video` of the `execution` as well :) – undetected Selenium Jan 19 '18 at 12:43
0

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.

Murthi
  • 5,299
  • 1
  • 10
  • 15
  • I get the same error: ElementNotVisibleException: element not visible (Session info: chrome=63.0.3239.132) (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 6.1.7601 SP1 x86_64) – CosimoCD Jan 19 '18 at 11:10