首頁  >  文章  >  後端開發  >  在Selenium Python中建立Web元素驅動方法

在Selenium Python中建立Web元素驅動方法

PHPz
PHPz轉載
2023-08-19 09:29:051293瀏覽

What is Selenium?

One of the most well-known open-source frameworks for automating web browsers is called Selenium. It enables developers and testers to simulate user actions on a web page, such as clicking butsimulate user actions on a web page, such as clicking butsimulate, 設 out form, c​​ions, pagetons, 片場in order to test web applications or carry out repetitive tasks. For example, this could include clicking "Submit" on a form, clicking "Next" on a page, and so on.

有許多可以與Selenium一起使用的程式語言,包括Python、Java、C#和JavaScript。除此之外,它還為使用者提供了存取各種與網頁瀏覽器互動的工具和函式庫,如Chrome、Firefox、Edge和Safari。

一些Selenium的關鍵特性

  • 跨瀏覽器相容性 − Selenium支援多種網頁瀏覽器,讓您能夠在不同平台和瀏覽器上測試您的網頁應用程式。

  • Easy to use − Selenium provides a simple API that allows you to interact with web elements on a page, making it easy to automate repetitive tasks.

  • Extensible − Selenium can be extended with custom plugins and libraries, allowing you to add new functionality or customize the framework to meet your needs.

  • Large community − Selenium has a large and active community of developers and testers, who share their knowledge and contribute to the development of the framework

##Some key uses of Selenium

  • 自動化測試 − Selenium可以用於自動化功能和回歸測試網頁應用程序,使您能夠快速且一致地測試您的應用程式。

  • 網頁抓取 − Selenium可以用於從網頁中提取數據,讓您可以收集數據進行分析或研究。

  • Browser automation − Selenium can be used to automate repetitive tasks in a web browser, such as filling out forms 或 navigating to specific pages.

Overall, Selenium is a powerful and flexible framework for automating web browsers and testing web applications. It is widely used in the industry and has a strong community of developers and testersho contribute to wits ongorov opment.

在本文中,我們將討論如何使用Python建立Selenium網頁元素驅動方法。使用此方法,Selenium測試腳本能夠在網頁上建立網頁元素,然後對這些元素執行操作,例如點擊它們或在它們上鍵入。

Steps and processes

步驟1:導入所需的庫

在我們開始寫create_web_element驅動方法之前,我們需要導入必要的函式庫。在這種情況下,將使用Selenium Web Driver庫和時間庫。 Selenium Web Driver庫用於控制網頁瀏覽器,而時間庫用於在必要時為我們的腳本添加暫停。

文法

from selenium import webdriver
import time

Step 2: Creating the Web Element Driver Method

Now that we have our libraries imported, we can define the create_web_element driver method. This method will take in two parameters: the web driver instance and the element identifier.

文法

def create_web_element(driver, element_identifier)

This method creates a Selenium WebElement object from an element identifier and returns the object.

element = None
   try:
      element = driver.find_element(*element_identifier)
   except:
      print("Element 
   return element

The method starts by initializing an empty variable called an element. This variable will be used to store the web element object once it is created.

然後,該方法嘗試使用Selenium Web驅動程式提供的find_element方法建立web元素物件。 find_element方法接受兩個參數:定位機制和值。定位機制指定如何在網頁上定位web元素,而值參數是我們用來定位元素的值。

element = driver.find_element(*element_identifier)

In this driver method, we used an asterisk (*) before the element_identifier argument. This is to unpack the tuple that contains the locating mechanism and value parameters of the find_element method.##elementelement

該方法還包括一個try-except區塊。如果無法找到該元素,則該方法將在控制台列印錯誤訊息並傳回None。

Finally, the method returns the created web element object.

Step 3: Using the Web Element Driver Method

現在我們已經定義了create_web_element驅動程式方法,讓我們看看如何使用它來建立網頁元素並對其執行操作。在這個範例中,我們將為Google搜尋框建立一個網頁元素,將查詢輸入到搜尋框中,然後點擊搜尋按鈕。

# Create a Chrome web driver instance and navigate to Google
driver=webdriver.Chrome()
driver.get("https://www.google.com/")

# Create the search box and search button web elements
search_box = create_web_element(driver, ('name', 'q'))
search_btn = create_web_element(driver, ('name', 'btnK'))


# Type a query into the search box and click the search button
search_box.  send_keys("SeleniumWebDriver")
search_btn.  click()

# Wait for the search results to load and then close the browser.  sleep(5)
driver.  quit()

首先,我們建立一個Chrome瀏覽器驅動的實例,並導航到Google。然後,我們使用create_web_element驅動方法建立兩個網頁元素,一個用於搜尋框,一個用於搜尋按鈕。

然後我們使用搜尋框物件的send_keys方法來在搜尋框中輸入查詢。我們使用搜尋按鈕物件的click方法來點擊搜尋按鈕。

最後,我們新增一個暫停來等待搜尋結果載入完成,然後關閉瀏覽器

Which web driver command is used to check the presence of the web element?

isDisplayed() is the command to use.

The isDisplayed command in Selenium checks to see whether a certain element is present and whether or not it is shown. The value that is returned is true if and only if the element in question is visible.

What is the purpose of a web driver in Selenium?

You are able to conduct tests in several browsers with the help of Selenium WebDriver, which is a web framework. This programme is used for the purpose of automating the testing of web-based applications to ensure that they function as anticipated. While writing test scripts using Selenium WebDriver, you have the option of using a variety of programming languages.

在Selenium中有多少种类型的web驱动程序可用?

ChromeDriver, EdgeDriver, FirefoxDriver, and Internet Explorer Driver are some of the most important examples of implementation classes for the WebDriver interface. Every driver class is analogous to a different browser. Simply said, we create new instances of the driver class objects and operate on them. It enables you to run Selenium scripts on the Chrome browser more effectively.

Which API is used in Selenium WebDriver?

Log4J 1和Log4J 2是它们的应用程序编程接口。与Log4J 2相比,Log4J1具有许多非常好的功能,并且使用起来也非常愉快和多功能。

What is the structure of Selenium WebDriver?

The architecture of Selenium is made up of five different parts: the Selenium Client Library, the Selenium API, the JSON Wire Protocol, and Browser Drivers and Browsers themselves. A Selenium client library is a collection of Selenium instructions written in the programmer's language of choice and formatted according to the W3C Selenium protocol.

Which method of overriding is used in Selenium WebDriver?

方法重写是指子类的方法名与其基类中相应方法的名称相同的过程。这可以被视为一种方法,使得子类能够提供一个已经包含在其超类中并由其超类定义的方法的独特实现。

如何在Selenium中检查网页元素?

In order for us to be able to examine this element using Selenium, we need to discover a method to access it by right-clicking on the element and selecting the Inspect option from the context menu. The INPUT type is being scrutinised as part of our examination because it has an attribute of the NAME type. The NAME has a unique value.

Final Program, Code

from selenium import webdriver
import time
def create_web_element(driver, element_identifier):
   element = None
   try:
      element = driver.find_element(*element_identifier)
   except:
      print("Element not found.")
   return element element = driver.find_element(*element_identifier)
# Create a Chrome web driver instance and navigate to Google
driver = webdriver.Chrome()
driver.get("https://www.google.com/")

# Create the search box and search button web elements
search_box = create_web_element(driver, ('name', 'q'))
search_btn = create_web_element(driver, ('name', 'btnK'))

# Type a query into the search box and click the search button
search_box.send_keys("Selenium WebDriver")
search_btn.click()

# Wait for the search results to load and then close the browser
time.sleep(5)
driver.quit()

Output

在Selenium Python中创建Web元素驱动方法

结论

In this piece, we will walk you through the process of developing a universal function using NumPy. We began by gaining a knowledge of what a universal function is in NumPy as well as the significance of having one. First, we developed a simple Python function, and afterwards used the "numpy.frompyfunc" technique to transform it into a general-purpose function. After that, we used the "numpy.vectorize" method to specify the kind of data that would be returned by the universal function. In the last step of this process, we used the universal function on an array with several dimensions and then examined the resulting data. You will be able to improve the efficiency of your code by developing your own custom universal functions in NumPy by following these instructions.

以上是在Selenium Python中建立Web元素驅動方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除