看板 Python 關於我們 聯絡資訊
各位版上的大大晚上好 我最近在學習selenium以自動登入Gmail作為練習對象,已經卡關兩天囉。 在輸入ID的時候沒什麼問題。 但在輸入Password的時候卻發現抓不到原件 我有嘗試過使用Xpath跟CSS定位都失敗。 //*[@id="password"]/div[1]/div/div[1]/input //*[@id="password"]/div[1]/div/div[1]/div 這兩Xpath個我都嘗試過,也想過先用click在輸入,模擬真實輸入狀況。 但就是沒法輸入任何東西。下方是錯誤訊息。 NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="password"]/div[1]/div/div[1]/div"} (Session info: chrome=59.0.3071.115) (Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 6.1.7601 SP1 x86_64) 懇請高手指導一下應該如何正確定到位。 感謝。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.195.99.225 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1498997500.A.C01.html ※ 編輯: yimean (123.195.99.225), 07/02/2017 20:53:10
s860134: m...  _by_css_selector("[type='password']") 07/02 21:44
s860134: 瀏覽器 F12 可以看到原始碼,可找一些很明顯的屬性去定位 07/02 21:46
wennie0433: 如果確定定位沒錯 就加個等待時間 讓動作慢點 試試 07/03 01:50
感謝兩位大大的分享,但是我還是沒法正確輸入。 下方是我的程式碼 from selenium import webdriver from selenium.webdriver.common.keys import Keys import time chromedriver = "D:\desktop\Python3\chromedriver" driver=webdriver.Chrome(chromedriver) driver.get("https://accounts.google.com/ServiceLogin/identifier?service=mail& passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&sc c=1&ltmpl=default&ltmplcache=2&emr=1&osid=1&flowName=GlifWebSignIn&flowEntry= AddSession") driver.find_element_by_xpath("//*[@id='identifierId']").clear() driver.find_element_by_xpath('//*[@id="identifierId"]').send_keys("yimean@gmai l.com") driver.find_element_by_xpath('//*[@id="identifierNext"]/content/span').click() driver.find_element_by_css_selector("[type='password']").clear() time.sleep(2) driver.find_element_by_css_selector("[type='password']").send_keys("xxxxxxxx") 然後錯誤訊息如下: InvalidElementStateException: invalid element state: Element is not currently interactable and may not be manipulated (Session info: chrome=59.0.3071.115) (Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 6.1.7601 SP1 x86_64) 在請大大指導一下了,感謝。 ※ 編輯: yimean (123.195.99.225), 07/03/2017 08:40:33
s860134: 當你輸入完帳號後,密碼那邊有 js 的動態改變 DOM 結構 07/05 01:40
s860134: 你可以帳號點完後 sleep 2秒後繼續就可 07/05 01:41
s860134: 你貼的 code sleep 再往前一行應該就可以動了 07/05 01:41
s860134: https://goo.gl/gM6f5q 可以參考前後變化 07/05 01:47
yimean: 太感謝大大了,我好好研讀一下。感恩。 07/05 16:54
IRONBLOOD: driver.find_element_by_xpath("html/body/div[1]/div[ 07/27 00:47
IRONBLOOD: 1]/div[2]/div[2]/form/div[2]/div/div/div[1]/div[1]/ 07/27 00:47
IRONBLOOD: div/div[1]/div/div[1]/input").send_keys("xxxx") 07/27 00:48