TA的每日心情 | 无聊 2017-11-26 18:46 |
---|
签到天数: 382 天 连续签到: 1 天 [LV.9]测试副司令
|
上Python代码:
# coding=utf-8
import unittest
from selenium import webdriver
from time import sleep
class Baidu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
def baidu_search(self):
driver = self.driver
driver.get("http://www.baidu.com")
driver.maximize_window()
sleep(2)
print "Title is ", driver.title
# news = driver.find_element_by_xpath(r"//*[@id='u_sp']/a[1]") # this is wrong...ID is not the "u_sp"
news = driver.find_element_by_xpath("//div[@id='u1']/a[1]")
print news.text
def tearDown(self):
self.driver.close()
self.driver.quit()
if __name__ == '__main__':
unittest.main()
-------------------分隔线-------------------
打印出百度首页中的“新闻”二字,但是奇怪的是用谷歌定位xpath的时候,定位出来的id是u_sp,运行代码的时候确实无法识别,id要为u1才能进行识别
SO,感觉特别奇怪,对此有相关了解的坛友们,麻烦给个友好的解释。谢谢!
|
|