2017-08-08 182 views
1

我已經寫了一個腳本在Python中使用硒到特定的頁面。有五個步驟來跨欄到達那裏。首先,我的瀏覽器必須點擊「按地址搜索」按鈕,然後在相應的框中輸入「街道名稱」和相應框中的「街道地址」,然後單擊搜索按鈕以進入目標頁面。但是,當我點擊「按地址搜索」按鈕時,第一次嘗試時出現錯誤。看來我是這樣做的,但無法弄清楚我犯的錯誤。預先感謝您看看它。 我,試圖腳本:Python硒無法點擊一個按鈕

from selenium import webdriver 
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.Chrome() 
driver.get("http://hcad.org/quick-search/") 
wait = WebDriverWait(driver, 10) 
driver.find_element_by_id("s_addr").click() 
driver.find_element_by_name('stnum').send_keys('8227') 
driver.find_element_by_name('stname').send_keys('FINDLAY ST') 
driver.find_element_by_link_text("Search").click() 
driver.quit() 

元素在其中的「按地址搜索」按鈕ID被找到:

<tbody><tr> 
<td class="auto-style1"></td> 
<td width="15" align="left" valign="top" class="auto-style1"></td> 
<td width="600" align="left" valign="top" class="auto-style1"> 

<input type="submit" id="s_acct" value="Search By Account">&nbsp; 
<input type="submit" id="s_addr" value="Search By Address">&nbsp; 
<input type="submit" id="s_name" value="Search By Owner Name"><table> 
<form id="search_button" name="search_button" action="/records/QuickSearch.asp" method="post"></form> 

元素在其中休息關鍵詞填寫框:

<tr align="middle"> 
<td colspan="1">  
<select name="TaxYear">       
<option value="2017">2017</option>    
</select></td>  
<td align="middle"> 
<input name="stnum" size="5" maxlength="5"></td> 
<td align="middle"> 
<input name="stname" maxlength="24"> 
</td><td align="middle"> 
<nobr><input type="submit" value="Search"></nobr> 
</td> 
</tr> 

錯誤我收到:

raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: 

回答

1

您需要等待iframe可用,並在可用時切換到該位置。

... 
wait = WebDriverWait(driver, 10) 
element = wait.until(EC.presence_of_all_elements_located((By.TAG_NAME, 'iframe'))) 
driver.switch_to.frame(driver.find_element_by_tag_name("iframe")) 
driver.find_element_by_id("s_addr").click() 
driver.find_element_by_name('stnum').send_keys('8227') 
driver.find_element_by_name('stname').send_keys('FINDLAY ST') 
... 

此外,使用xpath似乎是一種更方便的方式來選擇搜索按鈕。

driver.find_element_by_xpath("//input[@value='Search']").click() 
driver.quit() 
+0

感謝Oluwafemi Sule,爲您的有效答案。我忘記了一段時間的iframe。它做了詭計。 – SIM