使用selenium web驱动程序在画布上绘制不起作用

时间:2017-10-02 13:16:00

标签: java selenium selenium-webdriver

我写了下面的内容,以便在CANVAS中绘制任何内容,但它无效。

Socket toA=new Socket("127.0.0.1",socketPortA); //connect to A
toA.getOutputStream().write(1);

在调试模式下,这可以工作但通常不会。有时候它有效,有时它不会。

因此DOM中有一个canvas元素,下面是HTML代码

 Actions builder = new Actions(browser);
 Action drawAction = builder.moveToElement(webElement,135,15) //start points x axis and y axis.
     .click()
     .moveByOffset(200, 60) // 2nd points (x1,y1)
     .click()
     .moveByOffset(100, 70) // 3rd points (x2,y2)
     .doubleClick()
     .build();
drawAction.perform();

运行代码时,有时绘制画布代码有效,有时则无效。

使用下面的代码等待canvas元素可见。

 <fieldset class="signature kyc-signature odd field_with_errors has-
   drawn" data-field="Registration::Field::Signature" data-field-
  name="registration[signatureImageData]" data-name="
  [["legalPerson","firstName"],["legalPerson","lastName"]]">
  <label class="fieldset-label" for="signatureImageData-1a9012cc">
  </label>
 <div class="signature-well">
 <input id="registration_signatureImageData" 
  name="registration[signatureImageData]" value="<?xml version="1.0" 
   encoding="UTF-8" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD 
   SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg 
  xmlns="http://www.w3.org/2000/svg" version="1.1" width="62" 
      height="52"><path fill="none" stroke="#000000" stroke-width="2" 
 stroke-linecap="round" stroke-linejoin="round" d="M 61 1 l 1 1"/><path 
 fill="none" stroke="#000000" stroke-width="2" stroke-linecap="round" 
  stroke-linejoin="round" d="M 1 51 l 1 1"/></svg>" type="hidden">
  <a class="clear" href=""></a>
  <div class="signature-container" data-sign-here="">
  <div style="padding:0 !important;margin:0 !important;width: 100% 
  !important; height: 0 !important;margin-top:-1em !important;margin-
  bottom:1em !important;"></div>
   <canvas class="jSignature" style="margin: 0px; padding: 0px; border: 
 medium none; height: 180px; width: 769px;" width="769" height="180">
  </canvas>
     <div style="padding:0 !important;margin:0 !important;width: 100% 
 !important; height: 0 !important;margin-top:-1.5em !important;margin-
 bottom:1.5em !important;"></div>
 </div>
 <div class="name-underlined"> </div>
</div>
<div class="signature-description"> </div>

1 个答案:

答案 0 :(得分:0)

 public void drawCanvas() {
        Actions builder = new Actions(browser);
        JavascriptExecutor executor = (JavascriptExecutor) browser;
        executor.executeScript("arguments[0].scrollIntoView();", webElement); //Scroll the page and move where the element is located
        Action drawAction = builder.moveToElement(webElement, 135, 15) //start points x axis and y axis.
                .click()
                .moveByOffset(200, 80) // 2nd points (x1,y1)
                .click()
                .moveByOffset(100, 100)// 3rd points (x2,y2)
                .doubleClick()
                .build();
        drawAction.perform();
    }