我正在尝试使用硒访问网站上的密码框。我尝试过find_element_by_xpath但没有运气

时间:2019-09-08 13:27:35

标签: python selenium xpath

尝试使用硒访问网站页面上的密码输入框

我尝试使用find_element_by_xpath,by_id等

import csv
from selenium import webdriver
from selenium.webdriver.support.select import Select
import pandas as pd

import requests
from bs4 import BeautifulSoup

option = webdriver.ChromeOptions()
option.add_argument("--incognito")
driver = webdriver.Chrome(executable_path=CHROMEDRIVER_DIR, options = option) 
driver.get("https://xxxxxxxxxxxxxx")
driver.maximize_window()

driver.implicitly_wait(10)
system = driver.find_element_by_xpath("//input[@id='_id:logon:CMS']")
system.send_keys('xxxxxx')

它引发的错误是

  

NoSuchElementException:消息:没有这样的元素:
  无法找到元素:
  {“ method”:“ xpath”,“ selector”:“ // input [@id ='_ id:logon:CMS']”}
  (会话信息:chrome = 76.0.3809.132)

2 个答案:

答案 0 :(得分:0)

问题在于表单位于ifame内部,您需要首先将驱动程序切换到该iframe。 (我还注意到,该输入的ID不是_id:logon:CMS而是_id0:logon:CMS

import csv
from selenium import webdriver
from selenium.webdriver.support.select import Select
import pandas as pd

import requests
from bs4 import BeautifulSoup

option = webdriver.ChromeOptions()
option.add_argument("--incognito")
driver = webdriver.Chrome(executable_path=CHROMEDRIVER_DIR, options = option) 
driver.get("https://analytics.aspire.qa/BOE/BI")
driver.maximize_window()

driver.implicitly_wait(10)
driver.switch_to_frame('servletBridgeIframe')
system = driver.find_element_by_xpath("//input[@id='_id0:logon:CMS']")
system.send_keys('xxxxxx')

此外,您无需在此处使用driver.find_element_by_xpath。在这种情况下,更方便的是使用driver.find_element_by_id

答案 1 :(得分:0)

您要查找的按钮属于<iframe>,因此您需要wait for presence of the iframe and switch to it

完成后,您应该可以找到所需的输入内容

示例代码:

driver.get("https://analytics.aspire.qa/BOE/BI")
wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.NAME, "servletBridgeIframe")))
system = wait.until(EC.element_to_be_clickable((By.ID, "_id0:logon:CMS")))
system.clear()
system.send_keys("foo")

更多信息:How to use Selenium to test web applications using AJAX technology