python - Not able to assign local variable value to Global variable in Python Selenium automation - POM
问题描述
I have a global variable named global_my_gift_card = "", and I want to update the value of the same variable from local variable (created inside method), I am trying to do this, but global variable is not getting updated.
Below is my code
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
class Create_Giftcard:
giftcardtype_dropdown_xpath = "//select[@id='GiftCardTypeId']"
activated_checkbox_xpath = "//input[@class='check-box']"
generatecode_button_xpath = "//button[@id='generateCouponCode']"
cuponcode_field_xpath = "//input[@id='GiftCardCouponCode']"
recepiantname_field_xpath = "//input[@id='RecipientName']"
save_button_xpath = "//button[@name='save']"
global_my_gift_card = ""
def __init__(self,driver):
self.driver = driver
def test_create_new_giftcard(self, gc_type, gc_rec_name):
wait = WebDriverWait(self.driver, 30)
gctype = self.driver.find_element_by_xpath(self.giftcardtype_dropdown_xpath)
sel = Select(gctype)
sel.select_by_visible_text(gc_type)
time.sleep(2)
self.driver.find_element_by_xpath(self.activated_checkbox_xpath).click()
self.driver.find_element_by_xpath(self.generatecode_button_xpath).click()
time.sleep(3)
global global_my_gift_card
my_gift_card =
self.driver.find_element_by_xpath(self.cuponcode_field_xpath).get_attribute('value')
self.driver.find_element_by_xpath(self.recepiantname_field_xpath).send_keys(gc_rec_name)
self.driver.find_element_by_xpath(self.save_button_xpath).click()
self.global_my_gift_card = my_gift_card + ""
print(global_my_gift_card, "Outside")enter code here
解决方案
The global
statement that you used in the test_create_new_giftcard
function
global global_my_gift_card
indicates that an identifier belongs to the module's scope, but the global_my_gift_card
variable has been created in the Create_Giftcard
class scope. So you should remove the global
statement, and refer to your variable as Create_Giftcard.global_my_gift_card
instead of self.global_my_gift_card
(you might want to rename it as well).
推荐阅读
- python - Python 脚本在 Jupyter 但不是命令行中工作,listdir 错误
- scala - 使用 Akka parquet 读取 fastparquet 文件
- r - 如何将单词列表(chr)与数据帧中多列中的值进行比较,并在 R 中匹配时输出二进制响应
- configuration - Hazelcast 中是否有与 Coherence tangosol.coherence.distributed.localstorage 配置等效的配置?
- python - 我想安装 maven 以使用软件调用 GROBID
- python - DataLoader num_workers 与 torch.set_num_threads
- python - Django 查询集:order_by ForeignKey 布尔属性
- curl - 如何使用 Curl 将多个文件上传到 Micronaut?
- php - 如何在codeiginter中使用base64_encode附加pdf
- firebase - 如何向我们的用户展示 Firebase Streamview