I have the following HTML code:
div id="flashMessage" class="error">
<span>Saved Section.</span>
<div id="errors" class="clearfix">
</div>
I would like to get the text which is contained in span, I tried all the locators.
Could someone please help me with the respective command and css or xpath locator in selenium, for the above query?
This should work for you:
string spanText = driver.findElement(By.cssSelector("div#flashMessage>span")).getText();
I'm not sure why the same locator wouldn't work for success messages, but you can try this:
string spanText = driver.findElement(By.cssSelector("div#flashMessage.msg>span")).getText();
Related
Critical UPDATE:
It appears that, when we reach the page by using Selenium, the read only fields never loads. It's a document.jsp page which loads. But when we reach the page manually, we get that data. I am using ChromeDriver. I think that explains why I am unable to retrieve the read only fields while using Selenium. If anyone knows of a work around, please let me know.
UPDATE: Since writing this question I have tried innerText again with CSS but it returns " " instead of "Bronze". So it looks like I am able to retrieve something. But it's . How can I get "Bronze"
I am trying to retrieve the text from the field of a read only element using ChromDriver. Below is the HTML code. I want to retrieve the String "Bronze"
<div class="column label-left" style="width:25%">
<div class="form-item clearfix null" id="attr_wrapper_1_offerType_t">
<label class="form-label" for="offerType_t" style="width: 130px"><span style="padding-right: 5px">Offer Type:</span></label>
<div class="form-element field-wrapper" id="field_wrapper_1_offerType_t" style="padding-left:130px">
<div class="field" message=""><span class="readonly-wrapper" id="readonly_1_offerType_t">New Business</span></div>
<div id="msg_1_offerType_t" class="error-hover" data-action-message="" message=""></div>
</div>
</div>
<div class="form-item clearfix null" id="attr_wrapper_1_dealClass_t">
<label class="form-label" for="dealClass_t" style="width: 130px"><span style="padding-right: 5px">Deal Class:</span></label>
<div class="form-element field-wrapper" id="field_wrapper_1_dealClass_t" style="padding-left:130px">
<div class="field" message=""><span class="readonly-wrapper" id="readonly_1_dealClass_t">Bronze</span></div>
<div id="msg_1_dealClass_t" class="error-hover" data-action-message="" message=""></div>
</div>
</div>
<div class="form-item clearfix attr-spacer" style="height: 25px;"></div>
<div class="form-item clearfix attr-spacer" style="height: 25px;"></div>
</div>
I am using id="readonly_1_dealClass_t" but it returns null.
I have also tried xpath="//span[contains(#id,"dealClass")]". It returns null too.
First of all, getText() on id, xpath, CSS all of them returns null. Then I tried all the below options.
I have also tried using JavascriptExecutor and retrieving the text(), but it doesn't help either.
I have also tried innerText and textContent for the above id but without success.
I have waits for 60 seconds until element is visible. It returns true. Which means its visible. But it just refuses to retrieve the string "Bronze".
I also tried getAttribute("value") too. Without success obviously.
I also thought I could use the id="field_wrapper_1_dealClass_t" and use innerText on it. Still no success. That one just returns a lot of whitespace.
What else can I try to retrieve the string "Bronze"?
PS: I don't have issues with Firefox. Chrome just refuses to go ahead. And business need is to stick with only Chrome right now. So I have to get this working in Chrome. Please help.
I hope I have been clear and I hope I have furnished enough HTML code.
UPDATE:
This returns [] for value, ie blank.
final String script = "return arguments[0].getAttribute('innerHTML')";
WebElement randomRow = driver.findElement(By.xpath("(//div[#class='field']/span)[21]"));
String value = (String) ((JavascriptExecutor) driver).executeScript(script, randomRow);
UPDATE 2:
This returns [ ] too:
String myText = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[#class='readonly-wrapper' and starts-with(#id,'readonly_') and contains(#id,'_dealClass_t')]"))).getAttribute("innerHTML");
As per the HTML you have shared to retrieve the String Bronze you have to induce WebDriverWait for the visibility of the element as follows :
String myText = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[#class='readonly-wrapper' and starts-with(#id,'readonly_') and contains(#id,'_dealClass_t')]"))).getAttribute("innerHTML");
I think you'll be able to get that using below by fetching 'innerHTML' attribute:
final String script = "return arguments[0].getAttribute('innerHTML')";
WebElement randomRow = driver.findElement(By.xpath("(//div[#class='field']/span)[21]"));
String value = (String) ((JavascriptExecutor) driver).executeScript(script, randomRow);
So, after several trial and error runs it turns out that the page does not load completely when logging into it using Selenium. I copied the URL which opens using Selenium and compared it with the one which opens manually. They were different. The part after formCreate right after document.jsp was completely different for the Selenium loaded page. So, to avoid too much complications, I used driver.get("url") with the "url" being the actual URL and then let it open. This loaded the fields correctly and I was able to do the validations.
I am trying to capture the product description from a webpage using ID and tagname but when I print it, it is showing blank. However I think I have used correct locators to locate the element.
Page Source
<div id="item-description-block" class="layout-container layout-container-background clearfix">
<h2>About this item</h2>
<div id="social_share" class="details-row" onclick="javascript:clickPDP('Share','123070751499');">
<div class="details-row clear_both">
<div id="inspirational_copy">
<p>The big plus: Our new formula now helps strengthen skin's own moisture barrier. More moisture stays in. Skin feels soft, springy. Has a healthy-looking glow.</p>
</div>
Web Driver Code
driver.get("http://www.debenhams.com/webapp/wcs/stores/servlet/prod_10701_10001_123070751499_-1");
WebElement description =driver.findElement(By.id("inspirational_copy").tagName("p"));
String description1 = description.getText();
I just tested it in Python and if you replace By.id("inspirational_copy").tagName("p") with a valid css selector you can then use getText() to get the text you're looking for.
driver.get("http://www.debenhams.com/webapp/wcs/stores/servlet/prod_10701_10001_123070751499_-1");
WebElement description =driver.findElement(By.cssSelector("div[id='inspirational_copy']>p"));
String description1 = description.getText();
I did notice when I arrived on the page I got a welcome message. This message prevented me from getting the text. After closing it I could get the element and the text without problems.
WebElement close = driver.findElement(By.cssSelector("button[title='Close']");
close.click()
You can solve this problem easily by using xpath as locator
driver.findelement(By.xpath("//div[#id='inspirational_copy']/p"));
String description1 = description.getText();
Hope this will get the text you want.
String description1 = driver.findElement(By.xpath("//*[#id="inspirational_copy"]/p")).getText();
I am trying to find an element on a page using Selenium. Here is some example content:
<body id="tinymce" class="mceContentBody " contenteditable="true" dir="ltr" style="overflow: auto;">
Here is how I am trying to select it:
driver.findElement(By.cssSelector("body#tinymce")).sendKeys("Hello, everyone!! Don't worry it is a test letter to check connection!!");
I do not get an element returned though.
It looks like you are testing against TinyMCE editor.
The issues are:
It's in an iframe, you need to switch into to the iframe first.
You need to send keys to <body> element (not <input>) inside that iframe
Here is what to do:
// switch to iframe, use locator of your choice, "#editMe_ifr" here as an example
WebElement editorFrame = driver.findElement(By.cssSelector("#editMe_ifr"));
driver.switchTo().frame(editorFrame);
WebElement body = driver.findElement(By.TagName("body")); // then you find the body
body.sendKeys(Keys.CONTROL + "a"); // send 'ctrl+a' to select all
body.SendKeys("Some text");
Further reading:
Interact with a cute editor using webdriver
Using C# with selenium and cleditor.
You can change your HTML to this:
<body>
<input id="tinymce" type="text"/>
</body>
And you can change the selector from body#tinymce to #tinymce. You shouldn't need to specify the tagname when using id because the id should be unique anyway.
How do i get "this text" from the following html code using Jsoup?
<h2 class="link title"><a href="myhref.html">this text<img width=10
height=10 src="img.jpg" /><span class="blah">
<span>Other texts</span><span class="sometime">00:00</span></span>
</a></h2>
When I try
String s = document.select("h2.title").select("a[href]").first().text();
it returns
this textOther texts00:00
I tried to read the api for Selector in Jsoup but could not figure out much.
Also how do i get an element of class class="link title blah" (multiple classes?). Forgive me I only know both Jsoup and CSS a little.
Use Element#ownText() instead of Element#text().
String s = document.select("h2.link.title a[href]").first().ownText();
Note that you can select elements with multiple classes by just concatenating the classname selectors together like as h2.link.title which will select <h2> elements which have at least both the link and title class.
I have multiple div's in a webpage URL that I have to parse which have the same class name but different names with no id's.
for eg.
<div class="answer" style="display: block;" name="yyy" oldblock="block" jQuery1317140119108="11">
and
<div class="answer" style="display: block;" name="xxx" oldblock="block" jQuery1317140119108="11">
I want to select data and parse from only one of the div's say namely (name="yyy") (the content inside the div's are <href> links which differ for each class.
I've looked up the selector syntax in the Jsoup webpage but can't get a way to work around it. Can you please help me with this or let me know if I'm missing something?
Use the [attributename=attributevalue] selector.
Elements xxxDivs = document.select("div.answer[name=xxx]");
// ...
Elements yyyDivs = document.select("div.answer[name=yyy]");
// ...