Selenium works locally, but can't click on a link on travis - selenium

I have the exact same version of selenium (2.53.6) and firefox (43.0) on a local virtualbox with Ubuntu trusty, and a trusty image on travis.
The HTML code is trivial
<div>
<i class="fa fa-close fa-2x" aria-hidden="true"></i><br>Close<br>
</div>
The test code is trivial as well
def test_start_stop_container(self):
driver = self.driver
driver.get(self.base_url + "/hub/login")
driver.find_element_by_id("username_input").clear()
driver.find_element_by_id("username_input").send_keys("test")
driver.find_element_by_id("password_input").clear()
driver.find_element_by_id("password_input").send_keys("test")
driver.find_element_by_id("login_submit").click()
driver.find_element_by_name("action").click()
self.wait_for(lambda: "noVNC" == driver.title)
driver.find_element_by_xpath("//i").click() # << this here.
self.wait_for(lambda: "noVNC" != driver.title)
driver.find_element_by_name("action").click()
driver.find_element_by_xpath("//i").click()
driver.find_element_by_xpath("(//button[#name='action'])[2]").click()
self.wait_for(
lambda: "Start" == driver.find_element_by_name("action").text)
driver.find_element_by_id("logout").click()
In both cases I use Xvfb, but only on Travis the click is not working. No exception happens. It just seems like the operation is not performed. I recorded the session on Xvfb using some ffmpeg magic, and what I see is that the link highlights in blue (which is the hover color) but then the link is not clicked.
This video shows the exact operation (starts around 20 sec mark)
Does anybody have an idea of what the problem could be, or if there's something I can do to debug it?

Actually some time click() method of WebElement doesn't work as expected due to some designing issue of the element or other issues. So in this case, here is an alternate solution provided by selenium to execute piece of JavaScript to perform further events on the element.
So can use execute_script() instead to perform click here as below :-
driver.execute_script("arguments[0].click()", driver.find_element_by_link_text("Close"))

Try this xpath to identify the href - "//a[contains(text(),'Close')]"

Related

Selenium element selection issues and VSCode bug? Automate the Boring Stuff chapter 12

I'm attempting to replicate this code:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://inventwithpython.com')
try:
elem = browser.find_element_by_class_name(' cover-thumb')
print('Found <%s> element with that class name!' % (elem.tag_name))
except:
print('Was not able to find an element with that name.')
But it keeps returning the exception. I'm running this on mac w. vscode and there are few things off.
find_element_by_class_name method doesn't seem to register as a method.
Everytime I run this Intellicode prompts get disabled
I can't run this at all on Chrome as it crashes the chrome browsers
I've also searched online for driver issues and have done webdriver with the chrome driver path. Didn't work either
This is the error I'm getting if run it without try and except.
elem = browser.find_element_by_class_name(' cover-thumb') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'WebDriver' object has no attribute 'find_element_by_class_name'
Selenium removed that method in version 4.3.0. See the CHANGES: https://github.com/SeleniumHQ/selenium/blob/a4995e2c096239b42c373f26498a6c9bb4f2b3e7/py/CHANGES
Selenium 4.3.0
* Deprecated find_element_by_* and find_elements_by_* are now removed (#10712)
* ...
You now need to use:
driver.find_element("class name", "VALUE_OF_CLASS_NAME")
Eg:
driver.find_element("class name", "cover-thumb")
But if a class name has multiple segments, use the dot notation from a CSS Selector:
driver.find_element("css selector", "img.cover-thumb")

Nightwatch.js: Drag and Drop

I am trying to test a drag and drop operation using Nightwatch.js 0.8.18, Selenium Server 2.53.0 and Chrome Driver 2.21.2.
Basically, I take the approach described at https://github.com/RobK/nightwatchjs-drag-n-drop-example/blob/master/spec/drag-and-drop.js – i.e.: something like ...
.moveToElement('some-xpath-expression', 10, 10)
.pause(100)
.mouseButtonDown(0)
.pause(100)
.moveToElement('other-xpath-expression', 30, 30)
.pause(100)
.mouseButtonUp(0)
The cursor moves to the element to be dragged (perceivable by the :hover style of the icon the mouse is over), but then nothing happens. It looks to me like the mouseButtonDown() action has no effect. (But who knows for sure?)
It makes no difference if I use Firefox instead of Chrome – the behavior is exactly the same.
Any ideas?
Guys you have to try this and it works fine in Chrome, Firefox and IE.
Just you have to install "html-dnd" using npm, as well as this is a link: https://www.npmjs.com/package/html-dnd
After installing you just have to execute this command
browser.execute(dragAndDrop, ['#draggable', '#droppable']);
For Example:
var dragAndDrop = require('html-dnd').codeForSelectors;
browser.execute(dragAndDrop,['#elemendId1','#elemendId2']).pause(2000);
Hope this will work fine for your test cases.
The moment you click the element the expression changes and thus the tests 'forgets' what they were supposed to be clicking.
It's recommended to use an action build approach as so:
http://elementalselenium.com/tips/39-drag-and-drop
Currently at Nightwatch Version 1.5.1 I'm able to drag and drop with the following example.
Example:
"Step 1: Drag and Drop": function (browser) {
browser.moveToElement('yourLocator', '#startingElement', 0, 0);
browser.mouseButtonDown(0);
browser.moveToElement('yourLocator', '#endingElement', 0, 0);
browser.mouseButtonUp(0);
}

Selenium selectors on SVG TSPAN elements fail with certain versions of PhantomJS

I am using Geb and Selenium, and noticed that tests that reference certain SVG elements fail on certain PhantomJS versions. This test running against the Highcharts demo site passes if I'm using PhantomJS 1.9.1, but fails on 1.9.7 - the SVG tspan element is successfully located (size() > 0 passes) but text() returns empty string.
I have been able to isolate that the problem is not Geb specifically - I get the same problem when I interact with the PhantomJSDriver directly.
So I don't where to go next to troubleshoot this: is it a problem in the PhantomJS remote driver, or in PhantomJS itself? How would I troubleshoot where the problem is?
import geb.spock.GebReportingSpec;
class TspanSpec extends GebReportingSpec {
def "tspan elements found but can't get text"() {
when:
go "http://www.highcharts.com/demo/bar-stacked"
then:
waitFor { $("g.highcharts-axis").find("tspan").size() > 0 }
$("g.highcharts-axis").find("tspan").text() == "Total fruit consumption"
}
}
Found a workaround: retrieve text with jQuery object instead of WebElement.
$("g.highcharts-axis").find("tspan").jquery.text() == "Total fruit consumption"

Explicit in selenium

I am using Selenium to get div value, but the fallowing code is not waiting for the page, just for URL. I used time.sleep, which is very primitive and totally not flexible. I want to change it on the explicit, but I am not too experienced in Python and I have a problem with that.
The website name has been changed just in case :
def repeat():
import wx
while True:
botloc = driver.find_element_by_id('botloc').text
print botloc
botX,botY = map(int,botloc.split(','))
print botX
print botY
wx.Yield()
def checker():
if driver.current_url == 'logged.example.com':
time.sleep(5)
repeat()
else:
checker()
checker()
How can I replace time.sleep with something flexible to wait the shortest time as possible after the page will be loaded? How to use explicit correctly with my code?
I know that's possible with using an element from the website, but I can't write anything sensible, I just need an example.
Is possibility to use element_by_id('botloc') for wait till it will be visible then start repeat() ?
How can i replace time.sleep with something flexible to wait shortest
time as possible after the page will be loaded?
I suppose you use get(url) to load the page. Generally you don't have to do anything, WebDriver automatically waits until page is being loaded. So you can remove time.sleep(). However there are some issues reported when loading the page using get with firefox driver, because of that you will have to wait for some target element which is supposed to be in the loaded page as mentioned below.
How to use explicit correctly with my code?
Have you checked Selenium webdriver documentation ? you can wait for botloc element explicitly as below
//assuming you have a valid webdriver reference
//Ex: DEFAULT_WAIT = 10 means
//waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds.
element = WebDriverWait(webdriver, DEFAULT_WAIT).until(EC.presence_of_element_located((By.ID, "botloc")))
Refer this page for more information

Manual input from user while running selenium IDE script

can user is able to give manual input while running selenium IDE script?
For ex. If there is name field then can we open input box everytime script runs so that user can give his input for name field?
Let me know whether it is possible or not..
If yes then please suggest me a solution.
Thanks in advance
You can use the following script to invoke a javascript prompt in order to get the value
<tr>
<td>storeEval</td>
<td>prompt("Please enter your FirstName")</td>
<td>firstName</td>
</tr>
Then accessing the value is a case of using ${firstName}.
EDIT: Since Selenium IDE 2.5.0, the script needs to look like this:
<tr>
<td>storeEval</td>
<td>javascript{prompt("Please enter your FirstName")}</td>
<td>firstName</td>
</tr>
As of 2020 using Selenium IDE 3.16.1, I found the following config worked by the suggested answers above. (I re-answered the question as above answers did not clearly combined all pieces)
Command : execute script
Target : return prompt("Question Text","Default value");
Value : yourVariableName
and fill in the input box by the stored var:
Command : type
Target : (your selector)
Value : ${yourVariableName}
In Selenium IDE 2.8.0:
Capture the test session entering the login and password. Edit the Command for each and change from "type" to "waitForText" and remove the values in the Value fields previously stored for login and password. Re-run the saved test script from the IDE.
Since you specifically asked about Selenium IDE, the answer is no. But you can pause the script and let the user type their name, then have the script continue. I've heard of folks using this technique for handling CAPTCHAs, which of course are not easily automatable.
You could use the technique suggested here which uses the Selenium WebDriver and Java's BufferedReader. I don't think it can be adapted for Selenium IDE, but the technique should work fine with Selenium RC.
The basic idea is:
Issue Selenium commands up to the point where you want to capture user input.
Call the following code to capture the user input to a BufferedReader.
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
reader.readLine();
Continue your Selenium commands
(Note, if you are using C#, there is a discussion here on how to use Console.In.ReadLine(); instead of BufferedReader).
Just a minor modification to Stephen Binns response. I'm running Selenium IDE 2.5.0 and my test looks like this:
<tr>
<td>store</td>
<td>javascript{prompt("password")}</td>
<td>password</td>
</tr>
Without the javascript{} it wouldn't prompt.
The proposed solution works fine for selenium IDE (tested for 2.5)
<tr>
<td>store</td>
<td>javascript{prompt("password")}</td>
<td>password</td>
</tr>
Use Thread.Sleep(20000);
This will freeze everything for 20s and you can enter whatever fields you want to enter manually in that time period
none of the accepted answers worked for me. Here is how I solved it:
Create new command (at the top to store my variable that I wanted to use in 30 places.)
Command: execute script
Target: return "Test Iteration #13"
Value: VarName
then in the 30 places I wanted to use that variable. I put this
Value: Enter some text into the web input box and my test iteration number is ${VarName}