卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16333本站已运行3317

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

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 buttons, filling out forms, and navigating between pages, 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 or 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 testers who contribute to its ongoing development and improvement.

在本文中,我们将讨论如何使用Python创建Selenium网页元素驱动方法。使用此方法,Selenium测试脚本能够在网页上创建网页元素,然后对这些元素执行操作,例如点击它们或在它们上键入。

Steps and processes

步骤1:导入所需的库

在我们开始编写create_web_element驱动方法之前,我们需要导入必要的库。在这种情况下,将使用Selenium Web Driver库和时间库。Selenium Web Driver库用于控制Web浏览器,而时间库用于在必要时为我们的脚本添加暂停。

语法

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.

该方法还包括一个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.

卓越飞翔博客
上一篇: 如何通过PHP和Vue.js实现移动端友好的统计图表
下一篇: 如何解决golang报错:panic: runtime error: index out of range
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏