本文共 2509 字,大约阅读时间需要 8 分钟。
xpath 或运算
model_node_list = self.driver.find_elements_by_xpath( '//*[@id="cc"]/div[6]/div[1]/ul/li/a[1]|//*[@id="cc"]/div[6]/div[2]/ul/li/a[1]')
获取父节点
t=node.find_element_by_xpath('..')
模拟hover事件
from selenium import webdriverfrom selenium.webdriver import ActionChainsfrom mysqlHelper import MysqlHeplerclass Proccessor: def __init__(self): self.driver = webdriver.Chrome("C:\\software\\chromedriver_win32\\chromedriver.exe") self.cnt = True pass def start(self): self.driver.maximize_window() self.driver.get("http://www.mtmchina.cn") node = self.driver.find_element('//*[@id="brands"]') model_hover = ActionChains(self.driver).move_to_element(node) model_hover.perform()proccessor = Proccessor()proccessor.start()
根据css属性选择节点
model_node_list = self.driver.find_elements_by_xpath('//*[@id="productfinder"]/ul/li[contains(@style,"display: block")]')
获取某一个元素下面的子元素
self.driver.maximize_window() self.driver.get("http://www.mtmchina.cn") li_node_list = self.driver.find_elements_by_xpath('//*[@id="productfinder"]/ul[2]/li') link_num = 1 for li in li_node_list: #获取当前元素下面的a元素 a = li.find_element_by_css_selector('a') href = a.get_attribute('href') if href == "http://www.mtmchina.cn/#": continue self.url_list.append(href) print(li.text, href) link_num += 1 print("总连接", link_num)
获取节点的class
cl = li.get_attribute("class")
设置节点Id
self.driver.execute_script('arguments[0].setAttribute("id","nurmemet_img_id");', img)
sel_items = self.driver.find_elements_by_xpath( '//*[@id="content_container"]/div[1]/a[contains(@class, "slider_image_selected")]')
info_side=self.driver.find_element_by_class_name("information-side")#information-side 下面的节点h1h1=info_side.find_element_by_css_selector("h1")# information-side下面的节点h2h2=info_side.find_element_by_css_selector("h2")#information-side 下面的节点clearfixclearfix=info_side.find_element_by_css_selector(".clearfix");#information-side 下面的节点product-feautured-specificationul=info_side.find_element_by_css_selector(".product-feautured-specification");
等待元素的出现
from selenium.webdriver.common.by import Byfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECwait = WebDriverWait(self.driver, 40) element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'information-side')))
转载地址:http://gbudl.baihongyu.com/