ホームページ  >  記事  >  バックエンド開発  >  Selenium Python で Web 要素駆動メソッドを作成する

Selenium Python で Web 要素駆動メソッドを作成する

PHPz
PHPz転載
2023-08-19 09:29:051294ブラウズ

セレンとは何ですか?

Web ブラウザを自動化するための最もよく知られたオープンソース フレームワークの 1 つは Selenium と呼ばれます。これにより、開発者やテスターは、ボタンのクリック、フォームへの入力、ページ間の移動など、Web ページ上のユーザー アクションをシミュレートできます。 Web アプリケーションをテストしたり、反復的なタスクを実行したりするため。たとえば、これには、フォームの [送信] をクリックしたり、ページで [次へ] をクリックしたりすることが含まれます。

Selenium で使用できるプログラミング言語は、Python、Java、C#、JavaScript など多数あります。これとは別に、Chrome、Firefox、Edge、Safari などの Web ブラウザーを操作するためのさまざまなツールやライブラリへのアクセスもユーザーに提供します。

Selenium のいくつかの重要な機能

  • クロスブラウザ互換性 - Selenium は複数の Web ブラウザをサポートしているため、さまざまなプラットフォームやブラウザで Web アプリケーションをテストできます。

  • 使いやすさ - Selenium は、ページ上の Web 要素と対話できるシンプルな API を提供し、反復的なタスクを簡単に自動化できます。

  • 拡張可能 - Selenium はカスタム プラグインやライブラリで拡張でき、新しい機能を追加したり、ニーズに合わせてフレームワークをカスタマイズしたりできます。

  • 大規模コミュニティ - Seleniumには開発者とテスターの大規模で活発なコミュニティがあり、知識を共有してフレームワークの開発に貢献します

Selenium の主な用途

  • 自動テスト - Selenium を使用すると、Web アプリケーションの機能テストと回帰テストを自動化でき、アプリケーションを迅速かつ一貫してテストできます。

  • Web スクレイピング - Selenium を使用して Web ページからデータを抽出し、分析や研究のためのデータを収集できます。

  • ブラウザ自動化 - Selenium を使用すると、フォームへの入力や特定のページへの移動など、Web ブラウザでの反復的なタスクを自動化できます。

  • 全体として、Selenium は、Web ブラウザを自動化し、Web アプリケーションをテストするための強力で柔軟なフレームワークです。業界で広く使用されており、継続的な開発と改善に貢献する開発者とテスターの強力なコミュニティがあります。

この記事では、Python を使用して Selenium Web 要素駆動メソッドを作成する方法について説明します。この方法を使用すると、Selenium テスト スクリプトは Web ページ上に Web ページ要素を作成し、これらの要素に対してクリックや入力などのアクションを実行できます。

ステップとプロセス

ステップ 1: 必要なライブラリをインポートする

create_web_element ドライバー メソッドの作成を開始する前に、必要なライブラリをインポートする必要があります。この場合、Selenium Web Driver ライブラリと Time ライブラリが使用されます。 Selenium Web Driver ライブラリは Web ブラウザの制御に使用され、Time ライブラリは必要に応じてスクリプトに一時停止を追加するために使用されます。

###文法### リーリー

ステップ 2: Web 要素ドライバー メソッドの作成

ライブラリをインポートしたので、create_web_element ドライバー メソッドを定義できます。このメソッドは、Web ドライバー インスタンスと要素識別子の 2 つのパラメーターを受け取ります。

###文法### リーリー

このメソッドは、要素識別子から Selenium WebElement オブジェクトを作成し、そのオブジェクトを返します。

リーリー

メソッドは、要素と呼ばれる空の変数を初期化することから始まります。この変数は、作成された Web 要素オブジェクトを格納するために使用されます。

次に、このメソッドは、Selenium Web ドライバーによって提供される find_element メソッドを使用して、Web 要素オブジェクトの作成を試みます。 find_element メソッドは、位置決めメカニズムと値の 2 つのパラメーターを受け入れます。位置決めメカニズムは、Web ページ上で Web 要素を配置する方法を指定します。一方、value パラメーターは、要素を配置するために使用する値です。

リーリー

このドライバー メソッドでは、element_identifier 引数の前にアスタリスク (*) を使用しました。これは、find_element メソッドの検索メカニズムと値パラメーターを含むタプルを解凍するためです。

このメソッドには Try-Except ブロックも含まれています。要素が見つからない場合、このメソッドはコンソールにエラー メッセージを出力し、None を返します。

最後に、メソッドは作成された Web 要素オブジェクトを返します。

ステップ 3: Web 要素ドライバーメソッドの使用

create_web_element ドライバー メソッドを定義したので、それを使用して Web 要素を作成し、それらに対して操作を実行する方法を見てみましょう。この例では、Google 検索ボックスの Web ページ要素を作成し、検索ボックスにクエリを入力して、検索ボタンをクリックします。

リーリー

まず、Chrome ブラウザ ドライバーのインスタンスを作成し、Google に移動します。次に、create_web_element ドライバー メソッドを使用して、検索ボックス用と検索ボタン用の 2 つの Web ページ要素を作成します。

次に、検索ボックス オブジェクトの 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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。