如何将参数传递给Selenium中的find_element?

huangapple go评论84阅读模式
英文:

How to pass the parameter into find_element in Selenium?

问题

Currently, I am practicing the utilization of Selenium for website testing. However, I have encountered an issue wherein I am unable to pass a parameter to a def function.

from booking.booking import Booking
with Booking() as test:

test.change_currency(currency='USD')`
def change_currency(self, currency=None):

currency_element = self.find_element(By.XPATH, '//*[@id="b2indexPage"]/div[1]/div/header/nav[1]/div[2]/span[1]/button')
currency_element.click()

selected_currency_element = self.find_element(By.?, '?')
selected_currency_element.click()

And here is the website that I try to test: https://www.booking.com/

Image of inspect

I have tried various ways by using By.CSS and By.XPATH methods.

英文:

Currently, I am practicing the utilization of Selenium for website testing. However, I have encountered an issue wherein I am unable to pass a parameter to a def function.

from booking.booking import Booking
    with Booking() as test:

    test.change_currency(currency='USD')`
def change_currency(self, currency=None):

    currency_element = self.find_element(By.XPATH, '//*[@id="b2indexPage"]/div[1]/div/header/nav[1]/div[2]/span[1]/button')
    currency_element.click()

    selected_currency_element = self.find_element(By.?, '?')
    selected_currency_element.click()

And here is the website that I try to test: https://www.booking.com/

Image of inspect

I have try various ways by using By.CSS and By.XPATH methods.

答案1

得分: 0

你可以尝试使用以下代码在booking.com上轻松更改和测试不同的货币:

import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import selenium.webdriver.support.expected_conditions as EC

def change_currency(currency):
    currency_list = driver.find_element(By.CSS_SELECTOR, 'div[data-testid="All currencies"]').find_elements(By.TAG_NAME, 'button')

    for curr in currency_list:
        code = curr.text.split('\n')[1]
        if code == currency:
            curr.click()
            return True

driver = Chrome()
driver.get("https://www.booking.com/")
wait = WebDriverWait(driver, 10)

# wait and click to open the "Select your currency" pop-up
currency_button = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'button[data-testid="header-currency-picker-trigger"]')))
currency_button.click()

# wait and click to close the sign-in pop-up
sign_in = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'button[aria-label="Dismiss sign-in info."]')))
sign_in.click()

# change the currency to Thai Baht
change_currency('THB')
time.sleep(5)

如你所见,函数change_currency('THB')将货币更改为泰铢。同样,你可以通过在货币循环中调用此函数来为多个货币执行此操作。

英文:

You can try changing and testing with different currencies on booking.com easily with my implementation below:

import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import selenium.webdriver.support.expected_conditions as EC

def change_currency(currency):
    currency_list = driver.find_element(By.CSS_SELECTOR, 'div[data-testid="All currencies"]').find_elements(By.TAG_NAME, 'button')

    for curr in currency_list:
        code = curr.text.split('\n')[1]
        if code == currency:
            curr.click()
            return True

driver = Chrome()
driver.get("https://www.booking.com/")
wait = WebDriverWait(driver, 10)

# wait and click to open the "Select your currency" pop-up
currency_button = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'button[data-testid="header-currency-picker-trigger"]')))
currency_button.click()

# wait and click to close the sign-in pop-up
sign_in = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'button[aria-label="Dismiss sign-in info."]')))
sign_in.click()

# change the currency to Thai Baht
change_currency('THB')
time.sleep(5)

As you can see the function change_currency('THB') changes the currency to Thai Baht. Similarly, you can do this for multiple currencies by calling this function from a loop of currencies.

huangapple
  • 本文由 发表于 2023年5月15日 03:59:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定