我正在尝试测试网站的表单提交页面。提交按钮具有以下HTML:
<input id="ctl00_PlaceHolderMain_SubmitButton" class="SubmitButton" type="submit" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$PlaceHolderMain$SubmitButton", "", true, "", "", false, false))" value="Submit" name="ctl00$PlaceHolderMain$SubmitButton"/>
我已经通过id找到它并在我的测试中运行以下代码。由于某种原因,没有抛出任何错误。测试刚过,没有点击按钮。我在这里缺少什么?
self.click(10,
"id",
OigHotlinePageMap['SubmitButtonId']
)
我将click()方法存储在页面对象中:
def wait_until_element_clickable(self, waitTime, locatorMode, Locator):
element = None
if locatorMode == LocatorMode.ID:
element = WebDriverWait(self.driver, waitTime).\
until(EC.element_to_be_clickable((By.ID, Locator)))
elif locatorMode == LocatorMode.NAME:
element = WebDriverWait(self.driver, waitTime).\
until(EC.element_to_be_clickable((By.NAME, Locator)))
elif locatorMode == LocatorMode.XPATH:
element = WebDriverWait(self.driver, waitTime).\
until(EC.element_to_be_clickable((By.XPATH, Locator)))
elif locatorMode == LocatorMode.CSS_SELECTOR:
element = WebDriverWait(self.driver, waitTime).\
until(EC.element_to_be_clickable((By.CSS_SELECTOR, Locator)))
else:
raise Exception("Unsupported locator strategy.")
return element
def click(self, waitTime, locatorMode, Locator):
self.wait_until_element_clickable(waitTime, locatorMode, Locator).click()
我将定位器存储在UI地图字典中:
OigHotlinePageMap = dict(SubmitButtonId = "ctl00_PlaceHolderMain_SubmitButton"
)
答案 0 :(得分:1)
尝试使用类名,它应该有效:
driver.find_element(By.CLASS_NAME, 'SubmitButton')
您将在wait_until_element_clickable
内向您的代码中引入此内容:
if locatorMode == LocatorMode.CLASS_NAME
element = WebDriverWait(self.driver, waitTime).\
until(EC.element_to_be_clickable((By.CLASS_NAME, Locator)))
然后将其用作:
self.click(10, "class", OigHotlinePageMap['SubmitButtonClass'])
现在您可以将字典更改为:
OigHotlinePageMap = dict(SubmitButtonClass = "SubmitButton")
答案 1 :(得分:1)
因此HTML中的onclick()没有被触发,因为没有选择网站上的一个单选按钮。选择所需的单选按钮后,我可以单击提交按钮。
常规的click()方法通过CSS选择器工作。