I'm regularly hitting odd visible page text that karate cannot see, likely due to some funky JS magic that I don't fully understand.
Example image shows text on the page generated after clicking on a translate button:
I'm trying to assert that the translated text is present and correct on the page.
This is the selector:
#results-panel > div > div > div.thread > div > div.activity.panel.panel-default > div.panel-content > div > div:nth-child(2) > div:nth-child(1) > div > div.analysedText-translation > div > span:nth-child(3)
Example snippet of the element:
Using the wildcard {} or {^} doesn't work. eg waitFor('{^}A few random things about cats') returns a null
I played around with CSS selectors and am able to highlight the text using:
highlight('.analysedText-translation > div > span:nth-child(3)')[0]
I was thinking maybe using waitForText but not sure how to apply it. Any suggestions?
The reason the docs don't talk much about CSS selectors is that it is a standard. BTW this is open source, you are welcome to contribute pull-requests to improve the documentation.
UI automation is hard, I'm not going to claim that any framework makes it magically easier.
Suggestions:
if not already, start using the VS Code debugger, you can type things like highlight('div.panel-content') into the interactive console and play around with the page. see a video demo here (55:40) https://youtu.be/yu3uupBZyxc?t=3340
open the Chrome devtools console and type things like document.querySelector('div.panel-content') to see what gets matched
get a reference to any parent element and then you can "walk the tree": https://github.com/intuit/karate/tree/master/karate-core#tree-walking
If still stuck, follow this process so that we can fix anything in the framework if needed: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
Related
I am trying to automate Salesforce application Using Selenium and getting NoSuchelementException though XPATH is correct and valid for particular object. When i have searched the issue, it might be reason for Shadow DOM.
For EX:
So XAPTH i have written like,
driver.findElement(By.xpath("//input[#name='Name']")).sendKeys("Jams");
or
driver.findElement(By.xpath("//input[#id='input-299']")).sendKeys("Jams");
This XPATH is highlighting in Console as well. But while automating it throws nosuchelement error.
So while checking for ShadowDOM option, am getting option like this for Name Object.
#shadow-root(user-agent)
Shadowroot DIV
-- nothing mentioned in div. it just open and closed tags.
How to automate this?
You can check if there are any iframes in your Dom. Simply do //iframe in your page developer mode(F12)> elements tab > search (Ctrf+F) area. If there are any, you will get the number of iframes.
Now if your textbox is in any of iframe use below code to go inside particular iframe first
driver.switch_to.frame("<name or Id of frame>")
then come out to frame use below:
driver.switch_to.parent_frame()
Also, if the issue is not related to frames check below for shadow-root related issue:
you can check below for shadow-root element ( Question is for Java, but you can co-relate):
How to interact with the elements within #shadow-root (open) while Clearing Browsing Data of Chrome Browser using cssSelector
The website contents are mostly created now using javascript. You may have to wait for certain elements to load before doing anything to it.
https://seleniumbyexamples.github.io/wait
Yes, already tried with all kind of waits.
I have to click on the big "Dress" block on this site: http://automationpractice.com/index.php
I CAN get the /a> and the /li> elements with Xpath, but none of them are ever "clickable" or "visible"
The following does work but I need a more "use like" solution
((JavascriptExecutor)driver).executeScript("arguments[0].click();", driver.findElement(By.xpath("//a[#title=\"Dresses\"]")));
Again, already tried with waiting until clickable and visible, but it just timeout after 2 minutes.
The issue is that there are two elements that match your locator. Since you are using .findElement (singular), it returns the first one which happens to be the one that is hidden and stays hidden unless you hover the first nav item, "Women".
There are two ways to work around this:
Keep the same locator, use .findElements (plural) and click the second element.
driver.findElements(By.xpath("//a[#title='Dresses']")).get(1).click();
^ gets the second element, the first is index 0.
Modify your locator to find the element you want uniquely.
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("#block_top_menu > ul > li > a[title='Dresses']"))).click();
I prefer the second because you can add a wait and also because it's a CSS selector. CSS selectors are more compatible, more flexible, easier to read (simpler syntax), etc.
Additional note: The way to avoid this issue is to always check your locators in the browser. I use Chrome because I find it to be the easiest to use and most powerful. You can open the devtools using F12. Press ESC if the Console isn't shown. In the console you can test XPaths using $x(), e.g. $x("//a[#title='Dresses']") returns 2 elements. You can test CSS selectors using $$(), e.g. $$("#block_top_menu > ul > li > a[title='Dresses']") returns only one element.
I am writing an automation script for an avatar upload module with the following CSS locator:
input[accept="image/png,image/jpeg,image/gif,image/bmp"]
I am using Robot Framework's Wait Until Element Is Visible keyword to look for the locator above but is unsuccessful with the error:
Element 'css=input[accept="image/png,image/jpeg,image/gif,image/bmp"]' not visible after 30 seconds.
Increasing the timeout also doesn't work. Using the same in Chrome Dev Tools would successfully find the element. My guess is that the commas/slashes are messing with Robot's locator parsing. My question is: What is the correct way to write the locator?
Though present in the DOM, an element may not be visible/rendered. This is very often the case with file upload input elements - the UI renders something different, a button, div that had applied styling and fits in better with the overall design.
Thus a check is it visible will rightfully fail. Change your pre-usage approach to validate the input is in the HTML - this is actually the same as what you did in the browser's dev tools - with the Page Should Contain Element keyword, and proceed on success.
There is no problem with the CSS locator your are using. Maybe the element is in another iframe?
How to get rid of the code highlighting on oi-options, ng-model etc.
I'm not sure what's the name of this option.
I've found out what the option is. It's "Unknown HTML tag". I think they've been highlighted due to the fact my HTML code has AngularJS functions in it. You can turn it off by going to
Settings > Editor > Inspections > HTML > Unknown HTML tag
I am trying to capture and store some text that appears inside of an svg element such as
<svg>
<g>
<text>Value to get</text>
</g>
</svg>
I have a method that I am using and it works for other elements, but the Selenium WebElement class method getText does not return any text for an svg element such as above.
Here is what my xpath looks like for the above example
//*[local-name()='g']//*[local-name()='text']
I am able to use findElement(By.xpath(myXpath)), but then when I call .getText() on it, it does not return any value and it also does not throw any errors.
Is there something I am doing wrong, or possibly an alternative method?
Also something interesting to note, the above approach worked fine on a Windows 7 machine, but not on Mac.
Have you tried something simple like a CSS selector, driver.findElement(By.cssSelector("svg > g > text")).getText();? Have you checked to see how many matches there are on the page? Perhaps the first match that you are getting actually has no text inside. Open the Chrome dev console and type $$("svg > g > text").length and see if it returns more than 1.
Here are some CSS selector references:
CSS Selectors Reference
CSS Selector Tips