我使用selenium在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">
<input type="submit" id="s_addr" value="Search By Address">
<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:
答案 0 :(得分: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()