TA的每日心情 | 擦汗 昨天 09:05 |
---|
签到天数: 1048 天 连续签到: 1 天 [LV.10]测试总司令
|
页面结构
网易登入页
登入成功后的页面代码
代码
- <font size="3"> '''
- #我们对网页的操作没有报错不代表自动化没有问题
- #此时需要通过页面返回的信息做一个判断,来做人工的定位
- '''
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- import time
- driver = webdriver.Chrome()
- driver.get("https://www.126.com/")
- time.sleep(4)
- print('Before login............')
- title = driver.title
- print(title)
- nowURL = driver.current_url
- print(nowURL)
- #这是为了先却换到运行模块,不然会定位不到元素
- fname =driver.find_element(By.XPATH,'/html/body/div[2]/div[3]/div[1]/div/div[4]/div[1]/div[1]/iframe')
- driver.switch_to.frame(fname)
- #百度输入框输入内容
- driver.find_element(By.XPATH,"//input[@name='email']").clear()
- driver.find_element(By.XPATH,"//input[@name='email']").send_keys("username")
- driver.find_element(By.NAME,"password").clear()
- driver.find_element(By.NAME,"password").send_keys("password")
- driver.find_element(By.ID,"dologin").click()
- #driver.find_element(By.CSS_SELECTOR,"[data-loginname='loginEmail']")
- time.sleep(5)
- print('After login............')
- title = driver.title
- print(title)
- nowURL = driver.current_url
- print(nowURL)
- #此种方法不能点出text
- # user = driver.find_elements(By.ID,'spnUid')
- #后面的这3种方法都可以 取登入页面后用户账号信息来验证自动化是否正常
- # user =driver.find_element(By.XPATH,'//*[@id="spnUid"]').text
- # user =driver.find_element(By.XPATH,'/html/body/header/div[1]/ul[1]/li[1]/a//*[@id="spnUid"]').text
- user =driver.find_element(By.CSS_SELECTOR,"[id='spnUid']").text
- print(user)
- driver.quit()</font>
复制代码 异常
打印结果找不到text。
解决办法
通过疯狂百度,尝试了text,getAttribute通通失败。一位热心网友的回复救了我,他怀疑是元素定位不准确导致的。于是我只定位了第一个元素,.text获取成功。
- <font size="3">
- user =driver.find_element(By.XPATH,'/html/body/header/div[1]/ul[1]/li[1]/a//*[@id="spnUid"]').text</font>
复制代码
|
|