Selenium Web driver xpath, span locator - firefox

Selenium Webdriver. Looking to Locate New Article from following code. Please note this is under an iframe.
<img class="rtbIcon" src="/icons/16/app/shadow/document_add.png" alt="">
<span class="rtbText">New Article</span>
I have tried to locate with xpath and many other ways. But following is what I get everytime
Code : driver.findElement(By.xpath("id('RadToolBar1'):div:div:div:ul:li[3]:a:span:span:span:span"));
Result:
The given selector id('RadToolBar1'):div:div:div:ul:li[3]:a:span:span:span:span is either invalid or does not result in a WebElement. The following error occurred:
New article has no name, id so please if some one can help find me solution.

Your xpath seems to be wrong. The best way to get the xpath for any element on a page is by installing mozilla add on - Fire Bug. You can inspect any element using this add on and also copy the correct xpath of your element present on the page.
This should be your xpath -
driver.findElement(By.xpath("//*[#class='rtbText']"));
or
driver.findElement(By.linkText("New Article"));
One of these should work. Let me know if you face any problem.

Related

Can't get xpath to locate element

I have this piece of HTML and I'm trying to select the <a href> link using xpath.
<li class="footable-page-nav" data-page="next" aria-label="next"><a class="footable-page-link xh-highlight" href="#">›</a></li>
I need the selector to be reasonably specific since "footable-page-link" exists in multiple places in the HTML.
I've tried this:
//li[#class='footable-page-nav']/a[#class='xh-highlight']//#href
Selenium throws an error: selenium.common.exceptions.NoSuchElementException
If I shorten the xpath expression to //li[#class='footable-page-nav'] just to see if I'm on the right track then I get
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: element has zero size
What am I missing?
Try changing your xpath expression to
//li[#class='footable-page-nav']/a[contains(#class,'xh-highlight')]//#href
and see if it works.

Watir, locate element by custom attribute

I am using Watir 6.16 and I have come across this line of code
<div data-guid="SearchTitle" class="acme"></div>
Not sure how to locate such an element in Watir, I have tried this -
element(:search_title, custom_attribute: "SearchTitle")
But this returns nothing, so am I forced to use xpath or is there another way?
Kev
You can change that - into _, it would work. Look at the code below
browser.div(data_guid: 'SearchTitle')

Robot Framework: Issue in checking for a text after span

In Robot Framework, I am trying to check whether "XXX" exists in the source code as follows:
<button class='btn-aaa'>
<span class='bbb'>
XXX
</button>
I tried with the following codes but they failed:
Page Should Contain Element | //button/span/following-sibling::text()="XXX"
---> Valid xpath but encountering error in Robot Framework with error message "InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //button/span/following-sibling::text()="XXX" because of the following error: TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted to the desired type."
Page Should Contain Element | //button/span[#class='bbb']/following-sibling::contains(text(),"XXX")
---> Invalid xpath
Page Should Contain Element | //button/span[#class='bbb']/following-sibling::[contains(text(),"XXX")]
---> Invalid xpath
Could anyone please suggest the proper way to check for "XXX" using the right xpath in Robot Framework?
The following are the info of the tools utilised:
robot framework version 3.0.4
Selenium Version 3.14.1
Selenium2Library Version 3.0.0
Python 3.6.6
Edited:
I really appreciate that all of you are trying to help. Thank you very much.
I will try to provide as much info as I can, so that you can help me as well.
I tried with:
//button[normalize-space()='View Data']
Unfortunately, it found nothing.
There are other buttons but there is only one with the text "View Data".
Here are more detailed code. I don't know whether it will help comparing with the simplified one mentioned earlier:
<button class="btn btn-submit float-left" type="button" onclick="return doActionParam('https://www.blabla.com/blabla',{op:'viewBla',blaId:'wxyz', activeTab:getActiveTabName()})">
<!--groupParent.-->
<span class="bongobongo"></span>
View Data
<button>
Hope the info is more helpful this time. Thanks again.
Additional Info:
Browser: Mozilla Firefox 62.0.3 (64 bits)
Could you try this xpath: //button[normalize-space()='XXX']
Your test should be: Page Should Contain Element //button[normalize-space()='XXX']
The problem is that your page does not contain a span with the text "XXX". What it actually has is "newline, XXX, newline". Also, neither the span nor the button has a sibling (and thus, no following-sibling).
You can ignore the whitespace surrounding the text with normalize-space. For example, the following works for me when I create an HTML document identical to what you have in your question:
Page should contain element //button[normalize-space()='XXX']
This should work on the updated example in your code, by simply replacing XXX with View Data:
Page should contain element //button[normalize-space()='View Data']

selenium findElement by another thing than By.id

I'm quite new to this wonderfull tool that selenium is, and i'm trying to make some examples tests in my web app (html/JS).
I managed to select some (most) elements withtheir id with the command driver.findElement(By.id("elementId"));
but i'm unable to find some elements that do not have an id tag.
I tried these following lines without result, as i have have an
By.cssSelector("//img[#alt='smthg']")
By.xpath("//img[#src='path/to/img'")
a mix of the two aboce (alt and src in xpath and cssSelector
This element HTML code is
<img src="absolut/path/to/img.png" border="0" onclick="JSfunction(0)" alt="smthg" style="cursor: pointer;">
If somebody could help me, that would be very nice :)
Thanks, and have a good day !
You can use either of below
By.cssSelector("img[alt='smthg'][src*='path/to/img']");
or
By.xpath("//img[#alt='smthg' and contains(#src,'path/to/img')]")

jasmine-jquery toBeDisabled not working

I have got following html
<a disabled="disabled"><img alt="First" src="/Content/Images/Grid/disabledFirst.png"></a>
And I run following expect on this html
expect($(element)).toBeDisabled()
where element is the selector for above html. The expect fails. Further investigation lead to following code in jasmine-jquery-1.3.1.js
toBeDisabled: function(selector){
return this.actual.is(':disabled');
},
which for some reason is returning false. I'm sure I'm missing something very basic here but just not able to spot it.
This fiddle shows that jQuery only finds inputs not anchors when using :disabled. It's understandable because the anchor element doesn't have the disabled property

Resources