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
Selenium定位优快云登录页面元素
于 2021-10-31 18:14:16 首次发布
本文介绍了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的强大定位能力,同时提醒在某些情况下可能需要手动编写定位表达式。

最低0.47元/天 解锁文章
4897

被折叠的 条评论
为什么被折叠?



