将元组转换为自定义字符串
Convert a tuple into a custom string
我面临一个情况
这是一个class的例子
class SearchCustomerLocators:
txtEmail_id = (By.ID, "SearchEmail")
txtFirstName_id = (By.ID,"SearchFirstName")
txtLastName_id = (By.ID,"SearchLastName")
btnSearch_id = (By.ID,"search-customers")
所以当我调用自定义函数时...
self.click(*SearchCustomerLocators.txtEmail_xpath)
点击功能如下
def click(self, webelement):
#self._driver.execute_script("arguments[0].scrollIntoView();", webelement)
el = self._wait.until(expected_conditions.element_to_be_clickable(webelement))
self._highlight_element(el, "green")
el.click()
问题是 Click 函数只需要 1 个参数作为完整字符串 ('xpath', "//button[@type='submit']")
但是 *SearchCustomerLocators.txtEmail_xpath 返回的字符串与
一样
xpath //按钮[@type='submit']
如何操作字符串以获得自定义输出?
当我试图操纵这个 x = *SearchCustomerLocators.txtEmail_xpath
我收到错误 Can't use starred expression here
看来您只需要从以下位置删除 *
:
self.click(*SearchCustomerLocators.txtEmail_xpath)
成为:
self.click(SearchCustomerLocators.txtEmail_xpath)
下面是一个简单的示例脚本,用于单击此页面上的“提问”按钮。请注意,我不会亲自以这种方式进行设置,但我试图复制您在上面发布的功能。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.service import Service
import sys
sys.path.append('D:\Path\to\your\geckodriver.exe')
service = Service('D:\Path\to\your\geckodriver.exe')
driver = webdriver.Firefox(service=service)
class SearchCustomerLocators:
txtEmail_id = (By.ID, "SearchEmail")
txtFirstName_id = (By.ID,"SearchFirstName")
txtLastName_id = (By.ID,"SearchLastName")
btnSearch_id = (By.ID,"search-customers")
txtButton = (By.XPATH, "//a[contains(text(), 'Ask Question')]")
def click(self, webelement):
#self._driver.execute_script("arguments[0].scrollIntoView();", webelement)
el = self._wait.until(EC.element_to_be_clickable(webelement))
# self._highlight_element(el, "green")
el.click()
class MAIN:
def __init__(self):
self.driver = driver
self.click = click
self._wait = WebDriverWait(self.driver, 10)
def main():
MAIN().driver.get("
MAIN().click(MAIN(), SearchCustomerLocators.txtButton)
if __name__ == "__main__":
main()
回答有关星号意义的问题:
参见 PEP 448
https://peps.python.org/pep-0448/
一些可以尝试的例子:
https://www.w3schools.com/python/python_tuples_unpack.asp
我面临一个情况
这是一个class的例子
class SearchCustomerLocators:
txtEmail_id = (By.ID, "SearchEmail")
txtFirstName_id = (By.ID,"SearchFirstName")
txtLastName_id = (By.ID,"SearchLastName")
btnSearch_id = (By.ID,"search-customers")
所以当我调用自定义函数时...
self.click(*SearchCustomerLocators.txtEmail_xpath)
点击功能如下
def click(self, webelement):
#self._driver.execute_script("arguments[0].scrollIntoView();", webelement)
el = self._wait.until(expected_conditions.element_to_be_clickable(webelement))
self._highlight_element(el, "green")
el.click()
问题是 Click 函数只需要 1 个参数作为完整字符串 ('xpath', "//button[@type='submit']")
但是 *SearchCustomerLocators.txtEmail_xpath 返回的字符串与
一样
xpath //按钮[@type='submit']
如何操作字符串以获得自定义输出?
当我试图操纵这个 x = *SearchCustomerLocators.txtEmail_xpath
我收到错误 Can't use starred expression here
看来您只需要从以下位置删除 *
:
self.click(*SearchCustomerLocators.txtEmail_xpath)
成为:
self.click(SearchCustomerLocators.txtEmail_xpath)
下面是一个简单的示例脚本,用于单击此页面上的“提问”按钮。请注意,我不会亲自以这种方式进行设置,但我试图复制您在上面发布的功能。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.service import Service
import sys
sys.path.append('D:\Path\to\your\geckodriver.exe')
service = Service('D:\Path\to\your\geckodriver.exe')
driver = webdriver.Firefox(service=service)
class SearchCustomerLocators:
txtEmail_id = (By.ID, "SearchEmail")
txtFirstName_id = (By.ID,"SearchFirstName")
txtLastName_id = (By.ID,"SearchLastName")
btnSearch_id = (By.ID,"search-customers")
txtButton = (By.XPATH, "//a[contains(text(), 'Ask Question')]")
def click(self, webelement):
#self._driver.execute_script("arguments[0].scrollIntoView();", webelement)
el = self._wait.until(EC.element_to_be_clickable(webelement))
# self._highlight_element(el, "green")
el.click()
class MAIN:
def __init__(self):
self.driver = driver
self.click = click
self._wait = WebDriverWait(self.driver, 10)
def main():
MAIN().driver.get("
MAIN().click(MAIN(), SearchCustomerLocators.txtButton)
if __name__ == "__main__":
main()
回答有关星号意义的问题:
参见 PEP 448 https://peps.python.org/pep-0448/
一些可以尝试的例子: https://www.w3schools.com/python/python_tuples_unpack.asp