Python selenium从下拉列表中选择元素。元素不可见异常

2024-09-30 22:22:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试浏览以下网站link

我需要自动执行以下步骤:

1)选择正确的下拉表(下图左侧的第一个)。在

2)从下拉菜单中选择一个选项(Caraibi选项)。在

3)单击搜索按钮。在

下拉图片:左边的第一个(“Dove vuoi andare?”)。 enter image description here

HTML代码如下:

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

我用的是这个代码:

^{pr2}$

所以在这里我可以得到所有的选择(这是其中的一部分):

 [<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")> 

因此,当我试图通过可见文本选择选项时,例如“Caraibi”,我得到以下错误:

# 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)

谢谢你的帮助!!!在


Tags: value选项seleniumelementselectoptionddlocean
2条回答

错误说明了一切:

OUT: ElementNotVisibleException: element not visible: Element is nocurrently visible and may not be manipulated(Session info: crome=63.0.3239.132)

错误清楚地表明,您试图与之交互的元素不可见,因为它们是动态的。因此,我们必须归纳出Explicit Wait,使元素变得可见,如下所示:

^{pr2}$

控制台输出:

Option Caraibi clicked

您可以简单地单击select元素,然后单击required选项。这可能对你有帮助。在

 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()

如果以上代码是动态的,请更改其id。在

相关问题 更多 >