鱼C论坛

 找回密码
 立即注册
查看: 1586|回复: 7

[已解决]网页中元素的查找

[复制链接]
发表于 2021-7-18 16:38:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. <p class="tang-pass-footerBarULogin pass-link" title="用户名登录" data-type="normal" id="TANGRAM__PSP_11__footerULoginBtn" style="display: block;">用户名登录</p>
复制代码


这个是我在百度首页的元素审查中截取的,用户登录界面。
这里我想选取“用户名登录”,如何查找元素?用哪种方法?
最佳答案
2021-7-20 10:20:27
  1. from selenium import webdriver
  2. from selenium.webdriver import Firefox
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.common.keys import Keys
  5. from selenium.webdriver.firefox.options import Options
  6. from selenium.webdriver.support import expected_conditions as expected
  7. from selenium.webdriver.support.wait import WebDriverWait


  8. if __name__ == '__main__':
  9.     url = 'https://www.baidu.com'
  10.     browser = webdriver.Firefox()
  11.     browser.get(url)
  12.     # 创建wait对象,等待10s
  13.     wait = WebDriverWait(browser, 10)
  14.     # 等待【登陆】按钮出现
  15.     wait.until(expected.visibility_of_element_located((By.ID, 's-top-loginbtn')))
  16.     # 登陆按钮出现后点击
  17.     browser.find_element_by_id('s-top-loginbtn').click()
  18.    
  19.     # 等待出现【用户名登陆】,出现后点击
  20.     wait.until(expected.visibility_of_element_located((By.ID, 'TANGRAM__PSP_11__footerULoginBtn')))
  21.     browser.find_element_by_id('TANGRAM__PSP_11__footerULoginBtn').click()
  22.    
  23.     # 找到用户名输入框,输入账号名;找到密码框,输入密码;最后找到登陆按钮登陆。
  24.     browser.find_element_by_id('TANGRAM__PSP_11__footerULoginBtn').send_keys('user')
  25.     browser.find_element_by_id('TANGRAM__PSP_11__password').send_keys('password')
  26.     browser.find_element_by_id('TANGRAM__PSP_11__submit').click()



复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-19 19:28:00 | 显示全部楼层
这里我想选取“用户名登录”,如何查找元素?用哪种方法?

什么意思啊?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-19 21:05:07 | 显示全部楼层
  1. import requests
  2. from lxml import etree


  3. def main():
  4.     url = 'xxx'
  5.     headers = {'user-agent': 'firefox'}
  6.     r = requests.get(url, headers=headers)
  7.     html = etree.HTML(r.text)
  8.     result = html.xpath('//p[@title="用户名登录"]')
  9.     print(result)


  10. if __name__ == '__main__':
  11.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-19 22:45:38 | 显示全部楼层

谢谢啊,但是在这个我没有学过。我现在学习selenium,能用selenium编个程序我看看吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-20 09:09:25 | 显示全部楼层
江湖散人 发表于 2021-7-19 22:45
谢谢啊,但是在这个我没有学过。我现在学习selenium,能用selenium编个程序我看看吗?

把具体的网页URL发出来。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-20 09:30:55 | 显示全部楼层
suchocolate 发表于 2021-7-20 09:09
把具体的网页URL发出来。

https://www.baidu.com/
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-20 10:20:27 | 显示全部楼层    本楼为最佳答案   
  1. from selenium import webdriver
  2. from selenium.webdriver import Firefox
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.common.keys import Keys
  5. from selenium.webdriver.firefox.options import Options
  6. from selenium.webdriver.support import expected_conditions as expected
  7. from selenium.webdriver.support.wait import WebDriverWait


  8. if __name__ == '__main__':
  9.     url = 'https://www.baidu.com'
  10.     browser = webdriver.Firefox()
  11.     browser.get(url)
  12.     # 创建wait对象,等待10s
  13.     wait = WebDriverWait(browser, 10)
  14.     # 等待【登陆】按钮出现
  15.     wait.until(expected.visibility_of_element_located((By.ID, 's-top-loginbtn')))
  16.     # 登陆按钮出现后点击
  17.     browser.find_element_by_id('s-top-loginbtn').click()
  18.    
  19.     # 等待出现【用户名登陆】,出现后点击
  20.     wait.until(expected.visibility_of_element_located((By.ID, 'TANGRAM__PSP_11__footerULoginBtn')))
  21.     browser.find_element_by_id('TANGRAM__PSP_11__footerULoginBtn').click()
  22.    
  23.     # 找到用户名输入框,输入账号名;找到密码框,输入密码;最后找到登陆按钮登陆。
  24.     browser.find_element_by_id('TANGRAM__PSP_11__footerULoginBtn').send_keys('user')
  25.     browser.find_element_by_id('TANGRAM__PSP_11__password').send_keys('password')
  26.     browser.find_element_by_id('TANGRAM__PSP_11__submit').click()



复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-20 16:17:17 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 14:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表