Python硒选择元素从下拉列表中。元素不可见异常 [英] Python selenium select element from drop down. Element Not Visible Exception

查看:79
本文介绍了Python硒选择元素从下拉列表中。元素不可见异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试抓取以下网站



HTML代码如下:

 <选择名称= ctl00 $ ctl00 $ ctl00 $ ctl37 $ g_7e88f2a7_c220_4ba6_8ca8_49ca1297d22a $ cruiseFinderControl $ ddl_MacroArea onchange = javascript:setTimeout('__ doPostBack(\00l 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 类= 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>迪拜/阿联酋< / 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 =印度洋>大洋洲印第安纳州,马尔代夫,毛里求斯< / option>
< option value = ORIENTAL LANDS>东方< / option>
< option value = SOUTH AMERICA>南美洲< / option>
< option value = TRANSATLANTIC> Transatlantiche< / option>

< / select>

我正在使用以下代码:

 从selenium导入webdriver 
从selenium.webdriver.support.ui import选择
导入时间

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_Macro



$ b select.options

所以直到这里我都能获得所有选项(这是其中的一部分) ):

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

因此,当我尝试通过可见文字(例如 Caraibi)选择该选项时,我得到以下信息错误:

 #按可见文本选择
select.select_by_visible_text('Caraibi')


OUT:ElementNotVisibleException:元素不可见:元素当前为
可见,可能无法操作(会话信息:chrome = 63.0.3239.132)
(驱动程序信息:chromedriver = 2.33.506120
(e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform = Windows NT 6.1.7601 SP1
x86_64)

谢谢您的帮助!

解决方案

错误说明了一切:

  OUT:ElementNotVisibleException:元素不可见:元素当前不可见,可能无法操作(会话信息:crome = 63.0.3239.132)

该错误明确表明您尝试与之交互的元素不可见,因为y是动态。因此,我们必须对 element_to_be_visible 进行显式等待,如下所示:

从selenium.webdriver.support.ui导入webdriver
从selenium.webdriver.support导入WebDriverWait
从selenium.webdriver.common导入EC_b $ b的预期条件。通过导入按

驱动程序= 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 ='electrical-wrapper selectric-ddlMacroArea'])))) .click()
WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH, // div [@ class ='electrical-items'] // ul // li [text() ='Caraibi']))))。click()
print( Option Caraibi clicked)

控制台输出:

 选项Caraibi单击


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?").

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!!!

解决方案

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

这篇关于Python硒选择元素从下拉列表中。元素不可见异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆