day03 selenium自动化测试

测试京东自动搜索和自动获取数据

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

browser = webdriver.Chrome()
wait = WebDriverWait(browser, 5)

def get_page():
    url = 'https://www.jd.com/'
    browser.get(url)
    input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#key')))
    input.clear()
    input.send_keys('机器人')
    button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#search button.button')))
    button.click()
    time.sleep(3)

    return browser.page_source

def main():
    html = get_page()
    print(html)
    etree_html = etree.HTML(html)
    items = etree_html.xpath('//div[@id="J_goodsList"]/ul/li[@class="gl-item"]')
    for item in items:
        titles = item.xpath('.//div[@class="p-name p-name-type-2"]//em//text()')
        print(titles)
        title = ''.join(titles)
        print(title)
        print('*' * 20)

if __name__ == '__main__':
    main()

你可能感兴趣的:(day03 selenium自动化测试)