美文网首页
Selenium 记录

Selenium 记录

作者: WangLane | 来源:发表于2019-10-23 21:25 被阅读0次

滑动

last_height = driver.execute_script("return document.body.scrollHeight") 
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") 

按照文本寻找控件

https://stackoverflow.com/questions/35470171/click-button-by-text-using-python-and-selenium
https://stackoverflow.com/questions/12323403/how-do-i-find-an-element-that-contains-specific-text-in-selenium-webdriver-pyth

driver.find_elements_by_xpath("//*[contains(text(), 'My Button')]")
driver.find_element_by_xpath('//button[text()="Outliers"]')

设置页面加载超时时间

https://stackoverflow.com/questions/17533024/how-to-set-selenium-python-webdriver-default-timeout

driver.set_page_load_timeout(30)

模拟键盘输入(不需要控件的input)

from selenium.webdriver.common.action_chains import ActionChains 
actions = ActionChains(driver)
title = 'hahaha'
actions.send_keys(title)
actions.perform()

字符串的cookie转换成selenium使用的字典

def get_formatted_cookie(cookie_str):
    """
    传入浏览器的cookie字符串,返回selenium可用的cookie字典.
    """
    l = cookie_str.split(';')
    cookie_dict = {}
    for item in cookie_str.split(';'):
        name,value = item.split('=',1)
        name=name.replace(' ','').replace('\r','').replace('\n','')
        value = value.replace(' ','').replace('\r','').replace('\n','')
        cookie_dict={  
            'name':name,
            'value':value,
            "domain": ".douyin.com",  # google chrome
            #"expires": "",
            #'path': '/',
            'httpOnly': True,
            #'HostOnly': False,
            'Secure': False
        }
        #pprint(cookie_dict)
        yield cookie_dict

def main():
    driver = webdriver.Chrome()
    # 添加cookie(必须先访问该页面才可以添加).
    url = 'http://www.baidu.com'
    driver.get(url)
    for cookie_dict in get_formatted_cookie(cookie_str):
        try:
            driver.add_cookie(cookie_dict)
        except Exception as e:
            print(f'添加cookie失败:{e}')
            pprint(i)
            return
    # 这次访问才是携带cookie的
    driver.get(url)

寻找元素以...开头或者结尾的

这个比较经常遇到,实际上也是xpath的部分。比如遇到<button class="primary-btn-20ab0c"></button>
这个后面的字符串是自动生成的,可以这样匹配:

driver.find_element_by_css_selector('div[class^="DraftEditor-root"]')

如果是匹配结尾,可以这样匹配:

driver.find_element_by_css_selector('div[class$="DraftEditor-root"]')

等待元素出现

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException


WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//input')))

等待url改变

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException


WebDriverWait(driver, delay).until(lambda driver: driver.current_url == url)

最大化窗口

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument('window-size=2560,1440')
driver = webdriver.Chrome(chrome_options=options)

相关文章

网友评论

      本文标题:Selenium 记录

      本文链接:https://www.haomeiwen.com/subject/vyllvctx.html