标签:tag load rop ppa comm phone nts find mamicode
from selenium import webdriver
browser = webdriver.Chrome()
browser = webdriver.Firefox()
browser = webdriver.Firefox()
browser = webdriver.Edge()
browser = webdriver.PhantomJS()
browser = webdriver.Opera()
browser = webdriver.Safari()
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("https://www.taobao.com/")
print(browser.page_source)
browser.close()
打开淘宝并输出网页源码
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("https://www.taobao.com/")
input_first = browser.find_element_by_id(‘q‘) # 通过id名查找元素
input_second = browser.find_element_by_css_selector(‘#q‘) # 通过css选择器查找元素
print(input_first,input_second)
browser.close()
打开淘宝页面,并查找id名为‘q’的搜索框
八种元素定位方式
id定位: find_element_by_id()
class定位:find_element_by_class_name()
CSS定位:find_element_by_css_selector()
name定位: find_element_by_name()
tag定位:find_element_by_tag_name()
link定位:find_element_by_link_text()
partial_link定位:find_element_by_partial_link_text()
xpath定位:find_element_by_xpath()
还可以通过By进行查找
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
browser.get("https://www.taobao.com/")
input_first = browser.find_element(By.ID, ‘q‘)
print(input_first)
browser.close()
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("https://www.taobao.com/")
input_first = browser.find_elements_by_css_selector(‘.service-bd li‘)
print(input_first)
browser.close()
find_elements_by_xxx
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Chrome()
browser.get("https://www.taobao.com/")
search = browser.find_element_by_css_selector(‘#q‘)
search.send_keys("iPhone")
time.sleep(1)
search.clear()
search.send_keys("iPad")
search.send_keys(Keys.ENTER)
time.sleep(5)
browser.close()
打开淘宝,找到搜索框,先输入iPhone,清空后重新输入iPad,并按下回车进行搜索
from selenium import webdriver
from selenium.webdriver import ActionChains
browser = webdriver.Chrome()
browser.get("https://www.runoob.com/try/try.php?filename=jqueryui-example-droppable")
search = browser.switch_to.frame(‘iframeResult‘)
source = browser.find_element_by_css_selector(‘#draggable‘)
target = browser.find_element_by_css_selector(‘#droppable‘)
actions = ActionChains(browser)
actions.drag_and_drop(source, target)
actions.perform()
注:因为所要操作元素在一个iframe中,所以要先切换到该iframe,否则获取不到元素
标签:tag load rop ppa comm phone nts find mamicode
原文地址:https://www.cnblogs.com/myquark/p/13157699.html