#元素聚焦javascript
target = d.find_element_by_xpath(ele)
d.execute_script("arguments[0].scrollIntoView();", target)css
#彈出框點擊確認
d.switch_to.alert.accept()html
#headless模式java
from selenium.webdriver.firefox.options import Optionspython
options = Options()web
options.headless = Trueless
d = webdriver.Firefox(options=options) # headless 模式測試
或者ui
firefox_options = Options()
firefox_options.add_argument('--headless')
# d = webdriver.Firefox()
d = webdriver.Firefox(options=firefox_options)spa
#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定位
https://blog.csdn.net/xie_0723/article/details/51437650