I am attempting to retrieve and store text from a Sub Header element on a web page.
The HTML from the page is as follows:
<h5 class="page-sub-header-com page-sub-header-subtext">Customer #8107 - NGP_APPLICATION_017</h5>
I would just like to retrieve the text "Customer #8107 - NGP_APPLICATION_017".
Thanks in Advance
This should be very easy with cssSelector
By css = By.CssSelector("h5.page-sub-header-com.page-sub-header-subtext");
string element = Driver.FindElement(css).Text;
Solution provided by Saifur
By css = By.CssSelector("h5.page-sub-header-com.page-sub-header-subtext");
string element = Driver.FindElement(css).Text;
Related
I'm trying to validate image on website however can't access the url of the image.
Most websites will have the image with a "src" tag however this website has the url imbedded in another tag.
Iv used return driver.FindElement(By.ClassName("p-image__image--contain")); to access the element and used GetAttribute("style"); to access the image url. However the only information given was background-position: center center;
html format
When you use GetAttribute("style"), it will only return whatever was in the actual style attribute on the element, but many CSS attributes are actually applied using class stylesheets or other ways. So instead you should use the GetCssValue method, like so:
element.GetCssValue("background-image")
getCssValue(); will help in this case
WebElement img = driver.findElement(By.className('imgClass'));
String imgPath = img.getCssValue("background-image");
you will got full values in imgPath, Value like url("imageURL") then you need to use regex OR get values by slip function
I hope this scenario will help
How do I change image src that contain Parent tags using jquery?
Can you please tell how to retrieve the same using c#?
C# code:
string s;
s= "<span id=\"span1\"><table><tr><td><img src=\"/image/img01.jpg\"></td></tr>";
I have formatted above code to string and assigned to one label
label1.text=s.tostring();
Now i want to change the img src using jquery.
I'm new to jquery please any one help me to solve this.
* Note: i dont want to use id attribute for img tag.
provided you only have one image inside the span (like your example):
$('#span1 img').attr('src','img02.jpg');
There is two simple option, firstly use the code which given by "andy"
$('#span1 img').attr('src','img02.jpg');
or
<img src=\"/image/img01.jpg\" id="exampleimg">
$('#exampleimg').attr('src','img02.jpg');
$('#span1 img').attr('src', '/path/to/new/img.jpg');
That should do the trick for you.
edit - beaten to the punch by 2 mins!
I need some help figuring out how to do something.
I got this gallery (galleriffic) and some images that are store in Flicker.com so I used the flicker api to get the images but still add them manually to test the gallery.
Now I'm looking for a good way to insert the images into the html after I get them with the flicker api.
I found this htmltextwriter and used the function
Response.Write(GetDivElements());
but this is adding the div's on the top of the html and not inside the body tag.
my qustions is:
is HtmlTextWriter writer = new HtmlTextWriter(stringWriter) a good way to build html tags on the server side?
Is there a better way to add elements to the html other then Response.Write(""); ?
Here is what I do when I need to add mark-up.
in my page
<asp:PlaceHolder ID="MyPlaceholder" runat="server"></asp:PlaceHolder>
in my code behind
MyPlaceholder.Controls.Add(new Literal() { Text="<div>some markup</div>"});
I do it this way because:
1) you can put the PlaceHolder where you need it in the structure of your page
2) by adding a Literal at runtime to the Controls collection prevents ViewState getting bloated with it's contents.
If you are using the older style of asp.net, and not asp.net MVC, then you can just create a div with an id and runat="server". Then you can just write directly to the html.
aspx page
<div id = "DivINeedToAddStuffTo" runat="server" />
aspx.cs
DivINeedToAddStuffTo.InnerHtml = GetDivElements();
Also, I do not see anything wrong with using HtmlTextWriter to create your Html markup
You might try looking into Placeholders. That way you can create an instance of an image control and then add it your your placeholder.
Image myImg = new Image();
myImg.ImageUrl = "MyPicture.jpg";
myPlaceholder.Controls.Add(myImg);
You should be able to use the ASP literal control:
foreach (var item in items)
{
Literal literal = new Literal();
literal.text = item.html; //Assuming the item contains the html.
MyPlaceholder.Controls.Add(literal);
}
You could have that code before the page has rendered.
Hope that helps
Paul
EDIT
Sorry, I think I was mistaken, I thought you had the html with the link to the image(s) and not the actual image itself, Justin's answer would suit you if that's the case.
var ctrl = new WebControl(HtmlTextWriterTag.Div) { CssClass = "SomeClass" };
ctrl.Attributes["style"] = "float:left;display:inline-block;margin:3px;";
ctrl.Controls.Add(new Image
{
ImageUrl =
Page.ResolveUrl("image path here")
});
this.Controls.Add(ctrl);
I have a C# Form with WebBrowser object.
This object contains HTML Document.
And there is a link in that document that has no markers (no id and no name)
How can I access this element??
I tried to use this:
webBrowser1.Document.GetElementsByTagName("a")[n]
But it is not very useful, because if there will be some new link on the page, I'll need to rebuild all program.
I also can not do loops through document, or get a substring of Document.ToString() because then I can not click the link.
Would be great if you could give me some advice.
In this kind of situation the best idea is always to find an "Anchor", meaning - a place in the document that never change.
Lets say that
dada
Doesn't have an ID or Name, so the closest you can go is check if the parent of the element you're looking for has an ID.
<div id="parentDiv">
Some text
Some other stuff
The link you're looking for
</div>
That way you could get the parentDiv, which you know doesn't change, and then the A tag inside that parent (which should be permanent unless that website completely changes the structure which is one of the problems in parsing external HTML pages)
Shai.
you can use Html Agility Pack. and select links by xpath
HtmlWeb htmlWeb = new HtmlWeb();
HtmlDocument doc = htmlWeb.Load(/* url */);
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[#href"])
{
// do stuff
}
You should have some info on how to identify the link. it may be id or name or the text. If the text is always same then check the inner text of that link.
I have a web browser control, already navigated to a page. Now I want to search for a specific text in the page, and scroll to the first occurrence.
Being able to scroll to other occurrences is a bonus.
You can try this code:
webBrowser1.Select();
SendKeys.Send("^f");
I don't know if it works in a WebBroswer. We make the broswer(IE/FF/etc) window scroll to some text with the following code:
//source code of http://www.sample.com/sample.html
<html>
...
<span name="aim">KeyWord</span>
...
</html>
If I want the window to scroll to the "KeyWord", simply visit http://www.sample.com/sample.html#aim
Using string document = myWebBrowser.DocumentText to get the source code of the page, and search the text in the string, get its node name, and navigate it using #.
See this if it helps:
string PageSource = null;
PageSource = WebBrowser1.Document.Body.InnerHtml();
if (Strings.InStr(PageSource, stringtoFind) > 0) {
...insert an Anchor tag here and then use
WebBrowser1.Navigate to go to the the new URL with the #Anchor tag
} else {
...whatever else
}
One way...
Use the Ctrl + F Key to invoke Find, native to the WebBrowser Control?