寒假生活指导18


from
selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # 设置Edge浏览器选项 option = webdriver.EdgeOptions() option.add_experimental_option("detach", True) driver = webdriver.Edge(options=option) # 获取当前窗口大小 current_window_size = driver.get_window_size() # 获取屏幕大小 screen_size = driver.execute_script("return [window.screen.availWidth, window.screen.availHeight];") # 将浏览器窗口调整到与屏幕大小一致,模拟最大化效果 driver.set_window_size(screen_size[0], screen_size[1]) url = "http://localhost:8080/#/login" driver.get(url=url) import time time.sleep(5) input_user = driver.find_element(by=By.XPATH,value="//input[@type='text']") input_user.send_keys('syh') input_password = driver.find_element(by=By.XPATH,value="//input[@type='password']") input_password.send_keys('123456') time.sleep(3) login_button = driver.find_element(by=By.XPATH,value="//button[@type='button']") # 点击按钮 login_button.click() time.sleep(3) the_next_btn=driver.find_element(by=By.XPATH,value="//li[@id='jiben']") the_next_btn.click() time.sleep(3) add_btn=driver.find_element(by=By.XPATH,value="//button[@id='add']") add_btn.click() time.sleep(3) add_qiye = driver.find_element(by=By.XPATH,value="//input[@placeholder='请输入企业名称']") add_qiye.send_keys('syh公司') time.sleep(2) click_lei = driver.find_element(by=By.XPATH,value="//input[@placeholder='选择行业类型']") click_lei.click() time.sleep(1) click_lei_1 = driver.find_element(by=By.XPATH,value="//li[@class='el-select-dropdown__item'][1]") click_lei_1.click() time.sleep(1) add_address = driver.find_element(by=By.XPATH,value="//input[@placeholder='输入企业地址']") add_address.send_keys('河北省') time.sleep(2) add_submit = driver.find_element(by=By.XPATH,value="//button[@id='add_submit']") add_submit.click() time.sleep(1) time.sleep(5) # 退出浏览器 driver.quit()

python基于selenium实现自动处理登录。

  1. 设置Edge浏览器选项,并创建一个浏览器实例:

    python
    option = webdriver.EdgeOptions()
    option.add_experimental_option("detach", True)
    driver = webdriver.Edge(options=option)
    
  2. 获取当前窗口大小和屏幕大小,然后将浏览器窗口调整到与屏幕大小一致,模拟最大化效果:

    python
    current_window_size = driver.get_window_size()
    screen_size = driver.execute_script("return [window.screen.availWidth, window.screen.availHeight];")
    driver.set_window_size(screen_size[0], screen_size[1])
    
  3. 打开指定的URL:

    python
    url = "http://localhost:8080/#/login"
    driver.get(url=url)
    
  4. 进行一些交互操作,如输入用户名和密码,点击登录按钮:

posted @ 2024-01-26 19:39  一个小虎牙  阅读(15)  评论(0编辑  收藏  举报