今天在用python写平台页面自动化测试代码的时候,因为textarea使用的UEditor文本框,不同于一般的输入操作,直接根据element的相关属性名找到对应元素进行用户输入模拟,为了将值通过程序模拟的方式填入UEditor中,我在官方文档中找到了这段代码:UE.getEditor('editor').execCommand('insertHtml', '页面自动化测试')'
然后用Python执行这段代码,代码示例如下:driver.execute_script('UE.getEditor(\'editor\').execCommand(\'insertHtml\', \'页面自动化测试\')')
,完美的将值填入UEditor中,但是这样执行JS使得element.submit()这句无法提交UEditor中的value。但是通过手动点击提交按钮的操作却可以提交。所以我就想用python模拟提交按钮点击提交。然后写了如下代码:
submitBtn = driver.find_element_by_class_name("anniuchengse") #审查元素password的name
submitBtn.click()
发现程序报错:selenium python Message: unknown error: Element is not clickable at point(XXX,XXX)
百思不得其解,在google上搜到了解决方法,在此写出与大家一起共勉(原文地址:https://stackoverflow.com/questions/11908249/debugging-element-is-not-clickable-at-point-error):
这个错误产生可能存在一下三种原因:
1。这个元素被点击的时候不在页面可见范围内
解决方法:使用action或JavascriptExecutor使它可点
示例如下:
//Action
WebElement element = driver.findElement(By("element_path"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();
//JavascriptExecutor
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(250, 0)"); // if the element is on top.
jse.executeScript("scroll(0, 250)"); // if the element is on bottom.
//或者
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView()", Webelement);
2。这个页面在这个元素被点击之前刷新了
解决方法:让这个页面等待几秒再刷新
3。这个可被点击的元素被其他元素/蒙层所覆盖了
让代码等待覆盖该元素的其他元素消失后再执行点击操作。demo如下:
By loadingImage = By.id("loading image ID");
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
wait.until(ExpectedConditions.invisibilityOfElementLocated(loadingImage));
我看了这些解决方法之后,写了这么一条代码解决了这个ERROR。代码如下:
driver.execute_script("arguments[0].scrollIntoView()", submitBtn);
最后放出我写的一个类似注册页面的自动化测试代码:
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"//用来测试的谷歌浏览器驱动存放目录
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)#Firefox()
driver.get("要填写内容的url地址")
select = Select(driver.find_element_by_name("f_class"))
select.select_by_value('6')
select = Select(driver.find_element_by_id("s_class"))
select.select_by_value('108')
contact = driver.find_element_by_name("LeaderSubmission[contacts]")
contact.send_keys("fanTest002")
phone = driver.find_element_by_name("LeaderSubmission[phone]")
phone.send_keys("13699269673")
quotation = driver.find_element_by_name("LeaderSubmission[quotation]")
quotation.send_keys("111")
driver.execute_script('UE.getEditor(\'editor\').execCommand(\'insertHtml\', \'页面自动化测试\')')
submitBtn = driver.find_element_by_class_name("anniuchengse") #审查提交按钮的name
#print(submitBtn.get_attribute('value'))
driver.execute_script("arguments[0].scrollIntoView()", submitBtn); //使得提交按钮可点的重要代码
submitBtn.click()
print('注册成功')
print(driver.current_url)
至此,python编写页面自动化测试代码初体验结束,发现python还是蛮好用的,与君共勉哦~
最后,附上如何用python操作谷歌浏览器打开网址进行页面自动化测试的参考教程地址:http://www.cnblogs.com/drake-guo/p/6188366.html(感谢~)
下面内容转载自上述教程地址:
1.安装selenium
pip install selenium
2.下载chromedriver
点击下载
3.将解压后的chromedriver.exe放到chrome浏览器的安装目录下。(与chrome.exe放在同一目录下即可)
这样就可以运行测试代码了~撒花,正式完成这篇文章。感谢在过程中给我良多帮助的大大们~