Anyone please help me to construct the shortest xpath for the below one. I am writing the automation script using selenium.
This is the link I would like to click on from selenium:
<a class="LiveChat" onclick="openLiveChat()" title="Live Chat" href="#nogo">
<span class="ChatIcon"> </span>
<span>Live Chat</span>
And following is the xpath I got from firebug:
/html/body/div/div/div/table/tbody/tr/td[2]/table/tbody/tr/td/table/tbody/tr/td/div/a[2]/span[2]
I tried the following command which doesn't seem to work:
selenium.click("//a[#class='LiveChat']/span");
As i can see,Live chat is a link...
There is a much better option in selenium to click on links,you can use the following statement,
driver.findElement(By.linkText("Live Chat")).click();
Moreover,you should try and reduce the use of xpath in your code, a better option would be cssSelectors.....
you can click here for more info
Use the following Xpath to click on the Live Chat
//a[#class='LiveChat']/span[contains(.,'Live Chat')]
Related
I am trying to automatically select a file into an upload prompt.
Here is the code for the upload section of the website.
<input name="__RequestVerificationToken" value="rAyNscZYeYLEYTV1rodwr0lQr_0Eadpfd11lcuHUvJf83lM57R2vruocFLDPjK0axYr_XBYgqmgTXVH_V2qHGFpAq-zGx_mMm72XIUj4Z6HlUjcyz47Vepfjysur7CR0N8xgHMkjW1KRgb4K6w6VPQ2" type="hidden">
<li>
<input id="imageUpload" class="hideupload" name="imageUpload" multiple="" type="file">
<a id="imageUploadLink" href="#"><i class="addContent_icon uploadlink"><img src="/Content/images/addContent-icon-images.png"></i>Upload Images</a>
</li>
The element of interest as far as I can tell is id: imageUploadLink. Clicking that will open the prompt to select file. Here is my code.
require "selenium-webdriver"
browser = Selenium::WebDriver.for :firefox
sign_in(browser, myUsername, myPassword) #signs into testing site
browser.find_element(:id, 'imageUploadLink').click
element = browser.find_element(:id, 'imageUploadLink')
element.send_keys "/Users/DanielScarlett/Desktop/Example.JPG"
send_keys seems to be typed into the bottom of the browser in a sort of find section that disappears quickly: http://imgur.com/a/7iXcW
I have tried many different ways to implement this, and nothing seems to work. Also, couldn't find anything anywhere to handle this.
I assume you want to select file via browse and then click on Upload Images link. If that is the case, ID for your interest is imageUpload.
Code is in Java, you can change for ruby.
It worked for me using :
driver.findElement(By.id("imageUpload")).sendKeys("absolutepathtoimage\\testImage.jpg");
Check out the use of not using click() on button. It triggers the OS level modal dialog where selenium fails to automate.
I referred it from How to upload file using Selenium WebDriver in Java answer given by #talktokets
When uploading files with Selenium you want to add the file path to the form (e.g., id: 'imageUpload') and then submit the form. You want to avoid triggering the system level dialog box since Selenium isn't able to handle this.
For Ruby, it would look like this:
file_upload = driver.find_element(id: 'imageUpload')
file_upload.send_keys('path/to/your/file')
file_upload.submit
You can see a full write-up that steps through this here.
And if you're looking to run your tests on a remote node (e.g., with Selenium Grid or on a third-party like Sauce Labs) then you'll want to take a look at the file_detector method.
I am trying to create the gmail account through watir. As part of it while I am trying to select the birthday using div element I am unable to do.
I tried with the below one:
#ie.div(:text,'May').click
My system configurations:
IE-8
Windows-7
I've always disliked seeing the answers on here that say 'Why are you doing this?" or "Why would you want to do this?". So I'll just say "Don't do this!". I'm sure whatever issue you are facing can be resolved without automating the creation of gmail account.
If all you need is a unique gmail address then see the link below:
http://www.codestore.net/store.nsf/unid/BLOG-20111201-0411
Another option is https://mailinator.com/
As #titusfortner indicates, that element cannot be selected/clicked because it is not visible. Using watir-webdriver, this snippet makes the dropdown menu visible so its options can be selected:
b.div(title: "Birthday").when_present.click
b.div(text: "May").when_present.click
That being said, there are two other (read: larger) issues:
You won't be able to script your way past the captcha.
Google doesn't want you doing this. That's why the captcha is there, and they'll eventually block your IP if you consistently automate/script against them.
You can't click on the text of something that is not visible on the page. You first need to open the drop-down:
browser.span(id: 'BirthMonth').click
browser.div(text: 'May').click
b.element(:css, '#BirthMonth > div').click
b.element(:text, 'May').click
It works with Chrome and with Firefox also but not perfect
I'm writing some tests for our web application. When I use this XPath inside the Firefox XPath-Checker plugin it just works fine:
//div[contains(#class, "my-class") and #data-custom-attr="id")]
But If use this in selenium code like so:
(new WebDriverWait(driver, MAX_TIMEOUT_FOR_ELEMENT_LOADING_IN_SECONDS)).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[contains(#class, \"my-class\") and #data-custom-attr=\"id\")]"));
a timeout exception is throwen.
Why does the xpath query works directly in the browser but not in the selenium test? Is there documentation on what I have to consider?
As side note: if I use the exact path /html/body/div/... etc. it does work. And a css selector pendant to my example above doesn't work either.
It should work, what's the max wait time you given in code.
I am testing an extjs web app by using robotframework and selenium2library, but I can not locate some HTML elements because they are assigned a dynamic id.
For example: selenium2library could not locate a button which html code like this:
<button type="button" id="ext-gen56" class=" x-btn-text silk-add">create vm</button>
I have tried to use detail XPath and css expression, but they do not work. How to deal with this problem? Any help would be appreciated.
What the tools I used are robotframework and selenium2library which are used for automatic web app testing.
Parts of my test script:
// filename is A.txt
//this is a resource file including keywords which are used by testcase file
*** Keywords ***
open vmmanager page
[Timeout]
click element xpath=//table[3]/tbody/tr[2]/td[2]/em/button
create vm
[Arguments] ${vm_name} ${input_description}
click element xpath=//table[3]/tbody/tr[2]/td[2]/em/button
click element xpath=//td[2]/table/tbody/tr[2]/td[2]/em/button
click link xpath=//tbody/tr/td[5]/div/a
click link xpath=//tbody/tr/td[7]/div/a
input text xpath=//div/div/input ${vm_name}
click image xpath=//div/div/img
click element xpath=//div[22]/div/div
click image xpath=//div[3]/div/div/img
click element xpath=//div[19]/div/div
name is null
alert should be present ${expect}
click button xpath=//div[2]/div/div/div/div/table/tbody/tr/td/em/button
click button xpath=//td[5]/table/tbody/tr[2]/td[2]/em/button
contact engineer
alert should be present
click button ext-gen311
//filename is B.txt
//this is testcase file
*** Settings ***
Resource fiel A.txt
*** Test Cases ***
VC_VM_01
open vmmanager page
create vm ${empty} description
name is null
VC_VM_03
open vmmanager page
create vm valentine day
contact engineer
I would just find out how to set static ids. If you dont have access to the application code, I would ask that the developers would assign static ids for the components. - this would be IMHO the simplest solution.
Generated ids just usually mean that no one yet cares about them.
Without IDs, you could test using _near and _under from Sahi automation framework...I am using it, its Open Source..have patience to try different ways to identify the elements...its cool and good...
Most of the elements will be having text present on them.
xpath=//button[contains(text(), 'create vm')]
A generic approach will be
xpath=//*[contains(text(), 'create vm')]
I am new to Capybara testing and am having a few issues.
I have a scenario I am trying to run, and this is the step implementation:
When /^I select the signin link$/ do
click_link 'Sign in'
end
I have tried to access this link with xpath, css, and tried the within implementation as well. Capybara cannot seem to find it, and returns a Capybara::ElementNotFound exception in all cases.
When I load the webpage without JavaScript, the link is not visible, and I'm wondering if this is why Capybara cannot find it. I found a trigger method, but am unsure how it works. Does anyone have a working example of trigger, or any other ideas for what I should do?
Are you using the selenium webdriver to run this test? It sounds like you are trying to run a scenario that requires javascript to see certain elements, without using a driver that supports javascript.
In your .feature file all you have to do is add this line before the scenario:
#javascript
Scenario: My Scenario
When blah blah blah
...
The #javascript tag tells capybara to use selenium-webdriver to run the test. It'll fire up firefox and go through the test - allowing all javascript functionality to work. This slows tests down considerably so only use it when absolutely necessary to test ajax-y and javascript-y behavior.
If that still doesn't work you can use this step:
Then show me the page
When I select the signin link
Which will open the page up for you in a new browser in that page's current state for your inspecting pleasure.