我正在使用 Selenium 嘗試自動登錄。驅動程式有 id 方法,但如果 html 頁面沒有 id,我應該使用哪種方法?
<input type="text" placeholder="Enter ID here (Example - someuser)" name="LoginID" required="">
<input type="password" placeholder="Enter your Password here" name="Password" required="">
<button type="submit">SUBMIT</button>
目前我有這個但我當然需要替換 id 方法只是不確定是什么
def login(url):
driver.get(url)
driver.find_element_by_id("text").send_keys("MyuserName")
driver.find_element_by_id("password").send_keys("somepassword")
driver.find_element_by_id("submit").click()
login(url)
uj5u.com熱心網友回復:
這些方法已被棄用:
warnings.warn(
"find_element_by_* commands are deprecated. Please use find_element() instead",
DeprecationWarning,
stacklevel=2,
)
您可以使用以下內容:
find_element(self, by=By.ID, value=None)
可能的搜索定位器如下:
class By(object):
"""
Set of supported locator strategies.
"""
ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"
對于您的代碼,請嘗試以下操作:
from selenium.webdriver.common.by import By
search_container = driver.find_element(By.CSS_SELECTOR, 'input[name="LoginID"]')
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/408356.html
標籤:
