Python使用Selenium无法定位目标站点登录邮箱、密码及登录按钮元素求助
前置注意事项
该站点登录表单嵌套在独立子iframe中,直接在根文档上下文定位会失败,需要先切换到对应iframe上下文,Python Selenium示例代码如下:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By # 等待iframe加载完成后切换 login_iframe = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//iframe[contains(@src,"login")]'))) driver.switch_to.frame(login_iframe)
各元素定位方式
切换iframe后可使用以下方式定位目标元素,均支持XPath、CSS选择器两种写法:
- 邮箱输入框
- XPath:
//input[@name="username" or @type="email"] - CSS选择器:
input[name="username"], input[type="email"]
- XPath:
- 密码输入框
- XPath:
//input[@type="password"] - CSS选择器:
input[type="password"]
- XPath:
- 登录提交按钮
- XPath:
//button[@type="submit" or contains(text(),"Zaloguj")] - CSS选择器:
button[type="submit"]
- XPath:
建议所有元素定位都搭配显式等待逻辑,避免页面未渲染完成导致的定位失败。
内容的提问来源于stack exchange,提问作者Favkes
相关产品推荐
相关产品推荐

