Selenium没有从下拉列表中选择一个选项

时间:2018-04-26 15:57:48

标签: python selenium selenium-webdriver web-scraping webdriver

我正试图从这个网站上搜集一些信息 http://www.dubaitrade.ae/ja-terminal-1

我必须转到终端代码栏并选择'General Cargo'

这是HTML。

SELECT SUM(value_tx), 
       CASE 
         WHEN value_type IN ( 'A', 'B' ) THEN 'A | B ' 
         ELSE value_type 
       END 
FROM   t 
GROUP  BY CASE 
            WHEN value_type IN ( 'A', 'B' ) THEN 'A | B ' 
            ELSE value_type 
          END 
ORDER  BY 2; 

我不确定这个问题是否是因为它嵌套在这个标签中。意味着它是某种形式。

<select name="terminal" id="terminal1" style="width:52%" tabindex="3"> 
<option value="GC">General Cargo</option>
<option value="T1" selected="selected">Terminal One</option>
<option value="T2">Terminal Two</option>
<option value="T3">Terminal Three</option></select>

我试过了

<form name="vesselform" id="vesselLinkFormID" method="post" action="/pmisc/vessel.do;jsessionid=1da4f04d50b1a09ff55171820810948e8430f1ad26af4f30e765658bb25b3ffa.e34NaxuKaxmOaO0OaxmKc34Sa3j0">

我总是收到无法找到元素的错误消息。

1 个答案:

答案 0 :(得分:0)

您的代码接近完美但在开始寻找<select>代码之前,您必须切换到所需的<iframe> id blockrandom 引导正确的 WebDriverWait 如下:

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver=webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("http://www.dubaitrade.ae/ja-terminal-1")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"blockrandom")))
Select(driver.find_element_by_xpath('//*[@id="terminal1"]')).select_by_visible_text('General Cargo')