我使用硒那樣的自動填寫網頁表單用Python寫一個腳本(幫助臺售票系統)與Python模擬按鍵動作的Linux
在這方面的一個因素是機票的身體沒有元素ID是Selenium能夠識別的,所以爲了輸入正文我必須找到標題元素,按Tab鍵,然後開始輸入正文。
下面是一些代碼,寫一條消息進入人體:
der = "/t this is the desc"
driver.find_element_by_id("title").send_keys(der)
的問題是,對於我這個代碼不工作。我真正需要做的是這樣的:
body = open(email.txt)
driver.find_element_by_id("title").send_keys("/t" + body)
所以我想它尋找標題元素,按tab鍵,然後寫什麼是儲存在體內變化成票的身體。唯一的問題是語法不好。
我看着SendKeys,但那只是windows。我正在使用Fedora 16.
任何幫助/建議將不勝感激。
謝謝!
你提到的票的身體不具有識別硒元素的ID。你能夠使用CSS定位器來選擇它嗎?使用driver.find_element_by_css_selector()或作爲使用xpath driver.find_element_by_xpath()的最後手段。 – Isaac
我找到了綁定文檔(http://readthedocs.org/docs/selenium-python/en/latest/locating-elements.html)並查看了查找元素的方法。我insatlled FireBug,並通過頁面。我最終找到了一個看起來很有希望的class_name,並最終運行。 我的代碼以driver.find_element_by_class_name(「ze_area」)結尾。send_keys(「this is the body」) –