Selenium file upload and pop-up processing

File Upload

The input tag can directly use send_keys (file address) to upload files

Usage:
el = driver.find_element_by_id('upload button id')
el.send_keys("file path + file name")

demo, Baidu picture upload as an example

from base import *


class TestUpload(Base):
    def test_upload(self):
        url = 'https://image.baidu.com/'
        self.driver.get(url)
        self.driver.find_element_by_xpath("//img[@class='st_camera_off']").click()
        self.driver.find_element_by_id("stfile").send_keys("D:\\my_project\\git_deve\\development\\img\\雪球自选设置测试用例.png")  # 发送文件
        time.sleep(3)


if __name__ == '__main__':
    pytest.main(["-vs", "test_upload.py"])

Bullet frame processing mechanism

Common methods of operating alert:

  • switch_to.alert(): Get the alert box on the current page
  • text: return text information in alert / confirm / prompt
  • accept() : accept an existing alert box
  • dismiss(): dismiss the existing warning box
  • send_keys(KeysToSend) : Send text to alert box. KeysToSend: Send text to the alert box

Test Case:

  • Open webpage  rookie tutorial online editor
  • Operate the page on the right side of the window, drag element 1 to element 2
  • At this time, there will be an alert box, click OK in the box
  • Then click to run
  • close page
from base import *
from selenium.webdriver import ActionChains


class TestUpload(Base):
    @pytest.mark.skip
    def test_upload(self):
        url = 'https://image.baidu.com/'
        self.driver.get(url)
        self.driver.find_element_by_xpath("//img[@class='st_camera_off']").click()
        self.driver.find_element_by_id("stfile").send_keys("D:\\my_project\\git_deve\\development\\img\\雪球自选设置测试用例.png")  # 发送文件
        time.sleep(3)

    def test_la(self):
        url = "https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable"
        self.driver.get(url)
        self.driver.switch_to.frame("iframeResult")  # 可以参考 https://www.cnblogs.com/c-keke/p/14942162.html
        draggable = self.driver.find_element_by_xpath("//div[@id='draggable']")  # 拿起
        droppable = self.driver.find_element_by_xpath("//div[@id='droppable']")  # 放下
        action = ActionChains(self.driver)  # 初始化动作链, 可以参考:https://www.cnblogs.com/c-keke/p/14928477.html
        action.drag_and_drop(draggable, droppable).pause(2).perform()
        time.sleep(1)
        self.driver.switch_to.alert.accept()  # 点击确定这个弹出框
        self.driver.switch_to.default_content()  # 返回默认的节点
        self.driver.find_element_by_xpath("//button[contains(text(),'点击运行')]").click()


if __name__ == '__main__':
    pytest.main(["-vs", "test_upload.py"])

over.

Finally: If you don’t want to experience the feeling of not being able to find information when learning, no one answering questions, and giving up after persisting for a few days, here I will share with you some learning resources for automated testing, hoping to give you some guidance on the way forward. Come to help, friends can get it for free if they need it 【保证100%免费】

Collection of software testing interview questions

Our advanced study of automated testing must be to find a high-paying job. The following interview questions are the latest interview materials from first-line Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. After completing this set of interview materials, I believe everyone can find a satisfactory job.

How to get the video file:

Guess you like

Origin blog.csdn.net/m0_75277660/article/details/130624577