亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我需要幫助識別用于 Selenium 的 Web 表單字段

我需要幫助識別用于 Selenium 的 Web 表單字段

白衣染霜花 2023-01-04 14:19:46
為了學習更多關于 Python、Selenium、Beautifulsoup 和一般網絡抓取的知識,我開始了一個小型個人項目,該項目最終應該能夠打開網頁的主屏幕、導航到搜索字段、輸入和搜索特定帳戶數字。目前,我已經成功地使用 Selenium 打開站點主頁,然后導航到包含我需要填寫的表單的頁面。我通過使用 Chromes 開發人員工具復制我需要單擊的區域的 xpath,然后將其輸入 Selenium 的&ldquo;查找元素&rdquo;和&ldquo;單擊&rdquo;功能來完成此操作。我遇到的問題是,當輸入我的代碼時,我嘗試單擊的特定字段的 xpath 會吐出以下錯誤Traceback (most recent call last):? File "C:/Users/WFrazierIII/PycharmProjects/Selenium Prcatice/Selenium Practice File.py", line 24, in <module>? ? driver.find_element_by_xpath('/html/body/form/table/tbody/tr[2]/td[2]/input').click()? File "C:\Users\WFrazierIII\PycharmProjects\Selenium Prcatice\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath? ? return self.find_element(by=By.XPATH, value=xpath)? File "C:\Users\WFrazierIII\PycharmProjects\Selenium Prcatice\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element? ? 'value': value})['value']? File "C:\Users\WFrazierIII\PycharmProjects\Selenium Prcatice\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute? ? self.error_handler.check_response(response)? File "C:\Users\WFrazierIII\PycharmProjects\Selenium Prcatice\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response? ? raise exception_class(message, screen, stacktrace)selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/form/table/tbody/tr[2]/td[2]/input"}? (Session info: chrome=83.0.4103.97)Process finished with exit code 1我的問題是,我應該努力為該字段找到合適的 xpath,還是應該嘗試使用不同的&ldquo;find_element&rdquo;來更好地使我的代碼工作。最后,我要補充一點,我對 Python 或 Selenium 知之甚少,對 HTML 幾乎一無所知(因此是蹩腳的入門項目),所以如果你認為有更好的方法來解決這個問題,那么我會喜歡這個指導。謝謝!
查看完整描述

3 回答

?
翻過高山走不出你

TA貢獻1875條經驗 獲得超3個贊

您面臨的問題是您要訪問的表單駐留在 iFrame 中。您必須明確告訴 selenium 切換到 iFrame。你可以這樣做。


driver = webdriver.Chrome('./chromedriver')


driver.get('https://www.alabamagis.com/Tallapoosa/')

driver.find_element_by_xpath('/html/body/center/font[2]/a').click()


time.sleep(6)


driver.find_element_by_xpath('/html/body/font/a[1]').click()


time.sleep(6)


# new code starts here

driver.switch_to.frame('searchFrame')

driver.find_element_by_name('Master__Account').send_keys('your desired input')

此外,查找所需輸入的更簡單方法是使用find_element_by_name. 如果您檢查要抓取的網站的 HTML 代碼,input field您要訪問的網站的name屬性值為Master__Account。您可以使用它來訪問輸入。


查看完整回答
反對 回復 2023-01-04
?
阿波羅的戰車

TA貢獻1862條經驗 獲得超6個贊

Account字段在 a 中,<frame>因此您必須:

  • 誘導WebDriverWait以等待所需的框架可用并切換到它。

  • 誘導WebDriverWait使所需的元素可點擊。

  • 您可以使用以下任一定位器策略:

使用CSS_SELECTOR:


driver.get('https://www.alabamagis.com/Tallapoosa/frameset.cfm?cfid=3986761&cftoken=54089502')

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"frame[name='searchFrame']")))

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='Master__Account']"))).send_keys("Sean Ken")

使用XPATH:


driver.get('https://www.alabamagis.com/Tallapoosa/frameset.cfm?cfid=3986761&cftoken=54089502')

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"http://frame[@name='searchFrame']")))

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "http://input[@name='Master__Account']"))).send_keys("Sean Ken")

注意:您必須添加以下導入:


from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions as EC

瀏覽器快照:

http://img1.sycdn.imooc.com/63b51ab30001831913650765.jpg

查看完整回答
反對 回復 2023-01-04
?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

您沒有將帳號發送到文本字段,試試這個 driver.find_element_by_xpath('/html/body/form/table/tbody/tr[2]/td[2]/input').send_keys('account_no')

send_keys 用于將輸入發送到文本字段和

driver.find_element_by_xpath('/html/body/form/table/tbody/tr[11]/td/input[1]').click()

點擊搜索按鈕


查看完整回答
反對 回復 2023-01-04
  • 3 回答
  • 0 關注
  • 353 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號