英文:
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/
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/
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论