我正在使用Python從網站上抓取數據。雖然我能夠使用Selenium登錄,但登錄後無法識別搜索字段。它顯示網頁加載了幀(不是iframe),但我無法使用搜索字段訪問該幀。我試圖改變幀到相關的框架(這似乎工作 - 沒有錯誤拋出),但如果我嘗試通過CSS/Xpath /名稱/ ID搜索搜索元素我得到一個NoSuchElementException。我正在使用Chrome Webdriver。硒 - 識別網絡元素
有什麼建議嗎?該頁面的源代碼如下:
<html>
<head>
<title> XYZ </title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="content-language" content="en" />
<script type="text/javascript">
if (navigator && navigator.appVersion && navigator.appVersion.match("Safari") && !navigator.appVersion.match("Chrome")) {
// hack to force a window redraw
window.onload = function() {
document.getElementsByTagName('html')[0].style.backgroundColor = '#000000';
}
}
</script>
</head>
<frameset id="wc-frameset" rows="82,*" frameborder="no" border="0" framespacing="0">
<frame frameborder="0" src="/frontend/header/" name="top" marginwidth="0" marginheight="0" scrolling="no" noresize="noresize" />
<frameset cols="*,156,850,*" frameborder="NO" border="0" framespacing="0">
<frame frameborder="0" src="/frontend/fillbar/" name="fillbar" marginwidth="0" marginheight="0" scrolling="no" noresize="noresize" />
<frame frameborder="0" src="/frontend/navigation/" name="navigation" marginwidth="0" marginheight="0" scrolling="no" noresize="noresize" />
<frame frameborder="0" src="/frontend/frames/" name="content_area" marginwidth="0" marginheight="0" scrolling="no" noresize>
<frame frameborder="0" src="/frontend/fillbar/" name="fillbar" marginwidth="0" marginheight="0" scrolling="no" noresize="noresize" />
</frameset>
</frameset>
</html>
是我到目前爲止的代碼是:
username = driver.find_element_by_id("username")
password = driver.find_element_by_id("password")
username.send_keys("****")
password.send_keys("****")
driver.find_element_by_class_name("bg-left").click()
#this bit works
driver.switch_to_frame("content_area")
#this seems to work too, got the frame name from the page source
search = driver.find_element_by_id("field-name")
search.send_keys("TEST")
#this fails, no element found
目標框的源代碼是:
<div id="field-name" class="field field-StringField">
<label for="name">Name</label> <div class="input-con"><input id="name" name="name" type="text" value=""></div>
</div>
我錯了嗎?當你搜索'id''field-name'時,你會得到'div'元素,但是你想發送擊鍵到'input'元素。那會不會是帶'id''name'的元素? –
我試過這種方式,但仍然有一個異常:selenium.common.exceptions.NoSuchElementException:消息:沒有這樣的元素:無法找到元素:{「method」:「id」,「selector」:「 name「} –
你的意思是'name = driver.find_element_by_id(」name「)'引發了異常? –