簡體   English   中英

使用Selenium和python登錄網站

[英]Logging into website using selenium with python

我正在嘗試使用硒登錄該網站:但是它說密碼和登錄名不可見。 我環顧四周,發現有人說要等待,但是等待似乎無濟於事。 這是我的代碼:

    # importing libraries
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import re, time, csv


driver = webdriver.Firefox()

driver.get("https://platform.openquake.org/account/login/")
driver.switch_to
driver.maximize_window
time.sleep(10)

username = driver.find_element_by_xpath("//input[@name='username']")
username.send_keys("hi there")

錯誤消息是:

ElementNotVisibleException: Element is not currently visible and so may not be interacted with

修改您的xpath:

username = driver.find_element_by_xpath("//div[@class='controls']/input[@id='id_username']")

您的XPATH實際上匹配兩個元素。 非復數驅動程序方法( find_element_by_XXX )返回它們找到匹配的第一個元素,在這種情況下不是您想要的。

在這種情況下,一個好的調試工具是使用復數形式( find_elements_by_XXX ),然后查看匹配的元素數量。

在這種情況下,您應該執行Tanu建議的操作,並使用限制性更強的XPATH:

username = driver.find_element_by_xpath("//div[@class='controls']/input[@id='id_username']")
password = driver.find_element_by_xpath("//div[@class='controls']/input[@id='id_password']")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM