在.Net中使用PhantomJS selenium webdriver时,Element.Click未执行

时间:2022-01-13 01:51:19

I am using our existing tool that works perfectly using the Firefox and Chrome implementations of the Selenium IWebdriver.

我正在使用我们现有的工具,它使用Selenium IWebdriver的Firefox和Chrome实现完美运行。

I am now doing some experimentation using the PhantomJS implementation. So far so good. However, as soon as I want to click a button it does nothing.

我现在正在使用PhantomJS实现进行一些实验。到现在为止还挺好。但是,只要我想点击一个按钮,它就什么都不做。

I can retrieve the element, however, looking closer at its properties the 'Selected' property states the following:

我可以检索元素,但是,仔细查看其属性,'Selected'属性声明如下:

    Error Message => 'Element is not selectable' caused by Request => {"headers":{"Accept":"application/json, image/png","Connection":"Close","Host":"localhost:37704"},"httpVersion":"1.1","method":"GET","url":"/selected","urlParsed":{"anchor":"","query":"","file":"selected","directory":"/","path":"/selected","relative":"/selected","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/selected","queryKey":{},"chunks":["selected"]},"urlOriginal":"/session/fcaf88a0-40b4-11e3-960d-bdce3224aacf/element/%3Awdc%3A1383063211142/selected"}

I would gather this is the cause that my click is not executed, however, I cannot make heads or tails from this error message. using Google did not help either.

我想收集这是我的点击没有被执行的原因,但是,我不能从这个错误消息做出正面或反面。使用谷歌也没有帮助。

Any help would be much appreciated.

任何帮助将非常感激。

Thanks in advance.

提前致谢。

1 个解决方案

#1


8  

We had a lot of similar issues with PhantomJS.

我们与PhantomJS有很多类似的问题。

So, couple of steps to figure out what the root cause to it

因此,有几个步骤可以弄清楚它的根本原因

  1. Set you screen size (as suggested in comments; PhantomJS uses 400x300 by default):

    设置屏幕大小(如注释中所示; PhantomJS默认使用400x300):

    driver.Manage().Window.Size = new Size(1920, 1080); //Size is type in System.Drawing"
    
  2. Use to verify that your element is actually visible:

    用于验证您的元素实际可见:

    new WebDriverWait(driver,
    TimeSpan.FromSeconds(timeOut)).Until(ExpectedConditions.ElementExists((By.Id(login))));
    
  3. Click on the element with Javascript

    使用Javascript单击元素

    IJavaScriptExecutor js = _driver as IJavaScriptExecutor;
    js.ExecuteScript("arguments[0].click();", buttonToClick); //buttonToClick is IWebElement
    

For Java it would be as follows:

对于Java,它将如下:

  1. Screen size

    屏幕尺寸

    driver.manage().window().setSize(new Dimension(width, height));
    
  2. Verifying element is visible

    验证元素是可见的

    WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("LOCATOR")));
    
  3. Clicking with JS

    单击JS

    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("arguments[0].click();", buttonToClick); //buttonToClick is WebElement
    

#1


8  

We had a lot of similar issues with PhantomJS.

我们与PhantomJS有很多类似的问题。

So, couple of steps to figure out what the root cause to it

因此,有几个步骤可以弄清楚它的根本原因

  1. Set you screen size (as suggested in comments; PhantomJS uses 400x300 by default):

    设置屏幕大小(如注释中所示; PhantomJS默认使用400x300):

    driver.Manage().Window.Size = new Size(1920, 1080); //Size is type in System.Drawing"
    
  2. Use to verify that your element is actually visible:

    用于验证您的元素实际可见:

    new WebDriverWait(driver,
    TimeSpan.FromSeconds(timeOut)).Until(ExpectedConditions.ElementExists((By.Id(login))));
    
  3. Click on the element with Javascript

    使用Javascript单击元素

    IJavaScriptExecutor js = _driver as IJavaScriptExecutor;
    js.ExecuteScript("arguments[0].click();", buttonToClick); //buttonToClick is IWebElement
    

For Java it would be as follows:

对于Java,它将如下:

  1. Screen size

    屏幕尺寸

    driver.manage().window().setSize(new Dimension(width, height));
    
  2. Verifying element is visible

    验证元素是可见的

    WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("LOCATOR")));
    
  3. Clicking with JS

    单击JS

    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("arguments[0].click();", buttonToClick); //buttonToClick is WebElement