Selenium定位优快云登录页面元素

本文介绍了Selenium定位网页元素的各种方法,包括id和name、class_name和tag_name、link_text和partial_link_text以及xpath和css_selector。强调了各种方法的优缺点,如id和name的唯一性,class_name和tag_name的非唯一性,link_text和partial_link_text在<a>标签中的应用,以及xpath和css_selector的强大定位能力,同时提醒在某些情况下可能需要手动编写定位表达式。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.action_chains import ActionChains

# 创建 WebDriver 对象,指明使用chrome浏览器驱动
wd = webdriver.Chrome(r'C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe')

# 调用WebDriver 对象的get方法 可以让浏览器打开指定网址
wd.get('https://passport.youkuaiyun.com/')
elements = wd.find_element(By.XPATH, "/html/body/div[2]/div/div[2]/div/div[1]/div[1]/div[1]/span[2]")
elements.click()
element = wd.find_element(By.CLASS_NAME, 'base-input-text')# 根据id选择元素,返回的就是该元素对应的WebElement对象

# 通过该 WebElement对象,就可以对页面元素进行操作了
# 比如输入字符串到 这个 输入框里
element.send_keys('Selenium_username')
element = wd.find_element(By.XPATH, "/html/body/div[2]/div/div[2]/div/div[1]/div[1]/div[2]/div/div[2]/div/input")
element.send_keys('Selenium_pw')
element = wd.find_element(By.XPATH, "/html/body/div[2]/div/div[2]/div/div[1]/div[1]/div[2]/div/div[4]/button")
e
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值