selenium 使用记录

#元素聚焦

target = d.find_element_by_xpath(ele)
d.execute_script("arguments[0].scrollIntoView();", target)

 

#弹出框点击确认
d.switch_to.alert.accept()

 

#headless模式

from selenium.webdriver.firefox.options import Options

options = Options()

options.headless = True

d = webdriver.Firefox(options=options) # headless 模式

或者

    firefox_options = Options()
    firefox_options.add_argument('--headless')
    # d = webdriver.Firefox()
    d = webdriver.Firefox(options=firefox_options)

 

 #xpath 定位

d.find_element_by_xpath('//*[@id="n_gw_links"]').get_attribute('value')

 

 #滚动条移动到底部

http://www.cnblogs.com/shaosks/p/6611248.html

js = 'var q=document.documentElement.scrollTop=999999'
d.execute_script(js)

 

 #下拉框选择

SM = Select(d.find_element_by_css_selector('.div_tab_title'))

SM.select_by_value(self.SB_name)

 

 #等待元素加载

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

 

locator = (By.XPATH, '//*[@id="lps"]/b[2]')
try:
  WebDriverWait(d, 20, 0.5).until(EC.presence_of_element_located(locator))

except:

  print('can not find report mess')

 

#等待网页加载

https://selenium-python-zh.readthedocs.io/en/latest/waits.html

driver.implicitly_wait(5)

 

获取HTML 标签内容

d.find_element_by_xpath('//*[@id="gsm_1_1"]').get_attribute('innerHTML')

返回元素内部所有代码,包括标签。

d.find_element_by_xpath('//*[@id="gsm_1_1"]').get_attribute('innerText')

返回元素内的text文本。

d.find_element_by_xpath('//*[@id="gsm_1_1"]').get_attribute('textContent')

 

根据text文本内容匹配

 d.find_element_by_xpath("//*[contains(text(),'InternetGatewayDevice.Time.zoneinfo')] 包含匹配

driver.findElement(By.xpath("//span[text()='新闻']"))     绝对匹配

 

 

 d.find_element_by_xpath("//*[contains(text(),'InternetGatewayDevice.Time.zoneinfo')]/..").find_element_by_css_selector('span:nth-child(3) > a:nth-child(1)').get_attribute("innerHTML"))

定位InternetGatewayDevice.Time.zoneinfo 文本对应的元素。然后根据这个元素再通过css_selector定位

 

 
commit = d.find_element_by_xpath('/html/body/div[2]/div[10]/div/a[1]')  #commit元素位置

d.execute_script("arguments[0].click();", commit)  #执行 js click
 
 
 

def javascript_click(d,element):

    """ 点击Edit 操作 """
    try:
        d.execute_script("arguments[0].click();", element)

    except:
        raise Exception("Could not click on locator " + element)
 
 
 
 
 

 https://blog.csdn.net/xie_0723/article/details/51437650

 

 

 

Python Selenium自动化测试详解

https://blog.csdn.net/huilan_same/column/info/12694

 

 

 

posted @ 2018-11-30 11:08  Ray_lei  阅读(273)  评论(0编辑  收藏  举报