In Selenium, does ClickAndHold work with webdriver and chrome? - selenium

I'm trying to make a click-and-drag event work using Selenium Webdriver (2.15). It works fine when using FF, but in Chrome it does not. In Chrome, it appears to have no effect. Here's what my code looks like:
Actions builder = new Actions(GuiOps.driver);
builder.MoveToElement(fromElem).ClickAndHold().MoveToElement(toElem).Release().Build().Perform();
Has anyone gotten ClickAndHold() to work successfully using Chrome?

Turns out this is a known issue. There's a workaround posted at http://code.google.com/p/chromium/issues/detail?id=92312 . Basically, you add a MoveByOffset(1,1) immediately after that ClickAndHold() call and it works.

First post/comment here. :)
Well, it worked for me. All you need to give is a bit of a pause after the "moveByOffset(1, 1)". It seems that the script is executing faster than it should. Here's a piece of code that worked for me:
"actions.clickAndHold(we).moveByOffset(1, 1).pause(1000).perform();". Hope it helps.

Related

Why does Selenium ExecuteScript method doesn't work anymore in Salesforce application but ExecuteAsyncScript works

Hi I've been trying to figure this for the past couple of days. I wrote the code below that use to work but doesn't work any more using the ExecuteScript method. The only thing i have changed was update my chrome driver because it was not launching chrome since the driver wasn't working with the older chrome version I have. So once I updated the chrome driver it began to work until i had to run this piece of code. I modified it below not to what the actual link text is.
IJavaScriptExecutor js = (IJavaScriptExecutor)WebActions.One;
js.ExecuteScript("alert('Welcome to Guru99');"); // This was added for testing purpose
IWebElement somelink = WebActions.One.FindElement(By.XPath("//span[text()='Some Text']"));
js.ExecuteScript("arguments[0].click();", somelink );
When i changed the method to be ExecuteAsyncScript it started working again. Is this something that has changed with the app or is this something i'm doing wrong. I tried reading up on ExecuteAsyncScript vs ExecuteScript to see the difference but all i was able to get was something about the call back telling
when its finished which i'm not experienced in java script.
I would just like to understand more then anything or should i just start using ExecuteAsyncScript method. Thanks
There's a chromedriver ticket about this issue here:
https://bugs.chromium.org/p/chromedriver/issues/detail?id=3103
It states:
The cause of the issue is this website modifies the constructor of the
built-in AsyncFunction object. This causes difficulty when
ChromeDriver tries to create an AsyncFunction to wrap the script being
executed.
chromedriver 78 fixes the issue. For testing with Chrome 77, the ticket recommends switching to executeAsyncScript, or using chromedriver 76 since "ChromeDriver vX will run with Chrome vX+1".

Finding element using Xpath worked in IE9 but not in IE11

Attached is the HTML SS I am trying to click a button in my application. The button text is " Reassign". Please note that there is a space before the text. The space is visible when looking in developer tool in IE9. But not in IE11. I am unable to find the element in IE11. Whereas a simple
Driver.findelement(By.xpath(".//button[text()=' Reassign']")).click()
Worked in IE9. Same statement is not working in IE11.
Instead of
Driver.findelement(By.xpath(".//button[text()=' Reassign']")).click()
you could try
Driver.findelement(By.xpath(".//button[normalize-space(text())='Reassign']")).click()
This will ignore any leading or trailing whitespaces.
Maybe using By.linkText() could help, as Microsoft may have implemented it different than By.xpath().
Try both By.linkText('Reassign') and By.linkText(' Reassign'). If neither works, try By.partialLinkText('Reassign'). This is of course not as specific, but, depending on your page, could do.
Check if the below xpath works
//div[#class='divbuttonholder margin-left-none margin- top1']/table/tbody/tr/td/button[#type='button'][#data-bind='click:$root.Reassign']

Selenium Silly GetAttribute Issue. Debug vs running code java

I'm using Eclipse/Java and interfacing with the Selenium chrome webdriver.
The code I'm executing is
Sting sValue = item.getAttribute("innerHTML");
If I am stepping over this code it works fine.
Otherwise, if I just run the code. It throws a NUllPointerException.
What gives? Any ideas?
Thanks!
It's possible the page doesn't have enough time to load when you run straight through, but when you step with the debugger it's just enough extra time for the page to finish loading and avoid the NullPointerException. Try adding a wait, as described in the accepted answer for this question: Getting Selenium to pause for X seconds.
This is not a real answer, it's just a total hack! I have to get some things done so I'll investigate predicates and WebDriverWaits later.
I was creating a list of webelements:
List wElements = getElements()
Then I was trying to get the innerHTML from each webelement in the list.
It works fine in debug mode, but when I execute the code it fails to gather the information IN SOME OF THE ELEMENTS in the collection. So what I did was retrieve the same list a second time.
List wElementsB = getElements()
Then I retrieved the innerHTML out of the wELementsB. Works fine.
I tried sleeping and telling the webdriver to wait. but those failed as well.
Thanks Guys!

click() not working occassionally in selenium

I had written a script in selenium webdriver which uses click() to click on a button which is a "dojo" element.
Previously it was working fine but its not working now, i have tried all the possible ways to make it work (using javascipt, waiting for element to be present, checking enabled and disabled options etc etc) but nothing seems to work.
I would really appreciate if anybody could help me out with this. thanks in advance. i am attaching the script and html code for ur reference.
public void Search_Project_Test(String Project_name,String Os_type,String Start_rel,String End_rel) throws InterruptedException, IOException
{
this.Software_type.click();
this.select_os_type(Os_type);
this.search_proj.sendKeys(Project_name);
com.wait_till_pageload();
SORA_Constants.driver.findElement(By.xpath("//button[#id='submit_project_search']")).click();
Thread.sleep(5000);
Try this ! And also check that element is visible on the page
SORA_Constants.driver.findElement(By.id("submit_project_search")).click();
IT worked finally, all I did is upgraded the FireFox version. i was using 23.0 and later switched to 26.0 which resolved the problem. i never knew if selenium had such kind of issues with different versions of firefox. Hope if anyone faces similiar kind of situation later, then this could b helpful. :)
Try this:
SORA_Constants.driver.findElement(By.xpath("//button[#id='submit_project_search']")).SendKeys("\n");
or:
SORA_Constants.driver.findElement(By.xpath("//button[#id='submit_project_search']")).SendKeys(Keys.Enter);

How to change the size of the browser window when running the FirefoxWebDriverProvider in JBehave Web

We're using JBehave Web to drive our selenium test suite for a new project and really like the Etsy.com example available on JBehave, especially the Java/Spring maven archetype as this fits in with our architecture.
The biggest problem so far has been documentation, which is why I'm posting here in the hopes that I can get some help from others in a similar situation.
It looks like JBehave Web only provides a "FirefoxWebDriverProvider" class and no corresponding one for Chrome. Has anyone else run into this problem? Have you written your own ChromeDriverProvider?
Also, we need to change the size of the browser that comes up by default and I can't seem to find a way of doing that during the bootstrapping of the test run.
We're using the Maven archetype: jbehave-web-selenium-java-spring-archetype which uses the jbehave-maven-plugin and the "run-stories-with-annotated-embedder" goal so we're using the "Annotated" method of extending the InjectableEmbedder.
If anyone can provide some guidance, I'd really appreciate it, even if just pointers to more examples.
How To Resize Window
webDriverProvider.get().manage().window().setSize(new Dimension(width, height));
You can easily find code like this by navigating through the code. If you are using Eclipse, Open Declaration and Quick Type Hierarchy options are everything you need.
How to Use Chrome Driver
You can use TypeWebDriverProvider or PropertyWebDriverProvider. For instance:
new TypeWebDriverProvider(ChromeDriver.class);
This should work:
driver.manage().window().setSize(new Dimension(800, 621));
What jokka said is correct, justr a side note: Before resizing window, I always put it to top left corner, so I know that WebDriver can "see" everything:
driver.get().manage().window().setPosition(new Point(0, 0));
Obviously, the driver above is assumed healthy instance of WebDriverProvider
We ended up finding this Chrome Driver and it's been working great. It can take a parameter when it is bootstrapped to start in maximized mode and also exposes the capability to add extensions when it starts up.
driver.Manage().Window.Size = new Size(x, y);
This works fine for me. The x and y are in a feature file.
Try this:
This is working fine for me.
Capybara.current_session.driver.browser.manage.window.resize_to(1800, 1000)