I am trying to read in the first description in the first row. However, for some reason my if statement is never becoming true. Here is the html code that has the class im trying to read from. I gave a picture of it. Below that is the actual code im running. There is a first page that I login to and then input the part number. This then takes me to this page where you see the html code in the picture. I then need to grab the description of the part number. I included a picture of the website I am grabbing the description from. In order to give you a better Idea of what's happening. Also, I have a web browser component in my code that I am using. Can you point out why its not working? Thanks.
var secondPage = webBrowser1.Document;
foreach (HtmlElement element in secondPage.All)
{
if (element.GetAttribute("className") == "SE-Content-PartSearch-Grid-Row-Description")
{
messagebox.Show("Found it");
}
}
ActualWebsite
HTMLCODE
I suggest that you put a breakpoint on the "if" statement. It will perhaps be easier to see what is going on if you modify the code slightly:
var secondPage = webBrowser1.Document;
foreach (HtmlElement element in secondPage.All)
{
String className = element.GetAttribute("className");
if (className == "SE-Content-PartSearch-Grid-Row-Description")
{
messagebox.Show("Found it");
}
}
Related
I have this code below where it goes through all of the links in a panel and once it is able find the relevant link, it will click it. The problem I have though is that when I select the link, the page reloads before it shows a new page. So I thought of adding a break to break out of the loop because if I don't add this, then it will give me a element is not attached to document error. I believe this error occurs because it is still trying to loop through the links whilst the page is reloading.
However, when adding the break and running the test, nothing happens as in it doesn't select a link. How can I break out of the loop after clicking the link?
I don't need to go back to the loop after the page has reloaded, I simply want to break out of the loop because the loop has done its job in finding the link and clicking.
public void SelectHomepageSearchPanelLink(string linkText)
{
var searchPanelLinks = _driver.FindElements(HomepageResponsiveElements.HomepageSearchPanelLinks);
foreach (var searchPanelLink in searchPanelLinks)
{
if (searchPanelLink.Text == linkText)
{
searchPanelLink.Click();
break;
}
else
{
throw new Exception($"{linkText} link not found by the responsive homepage search panel");
}
}
}
Try this:
public void SelectHomepageSearchPanelLink(string linkText)
{
var searchPanelLinks = _driver.FindElements(HomepageResponsiveElements.HomepageSearchPanelLinks);
var linkToClick;
foreach (var searchPanelLink in searchPanelLinks)
{
if (searchPanelLink.Text == linkText)
{
linkToClick = searchPanelLink;
break;
}
else
{
throw new Exception($"{linkText} link not found by the responsive homepage search panel");
}
}
linkToClick.Click();
}
I'm new in using mshtml package library from .NET and, as such, I'm struggling to read the input element rendered in WPF web browser control.
I have researched through the internet and stumbled on this link. How to get the value of an input box of the webbrowser control in WPF?
But in my case, I want to read/get the input element by class, not by name attribute. Unfortunately, I know that GetElementByClass is not an option in WebBrowser.Document
This is my code - When I tried to run the debugger, the code doesn't continue inside the foreach loop ):
private void browser_LoadCompleted(object sender, NavigationEventArgs e)
{
IHTMLElementCollection elementCollection;
IHTMLDocument3 dom = (IHTMLDocument3)browser.Document;
elementCollection = dom.getElementsByTagName("input");
foreach (HtmlElement item in elementCollection)
{
if (item.OuterHtml.Contains("input_search"))
{
//Do something else here...
}
}
}
A screen capture of the DOM of the page
Any help is greatly appreciated.
Ok this one got me hitting my head on the wall for a while now.
I look at a list of web elements. I access that list like so
foreach (IWebElement link in driver.FindElementsByCssSelector("span.cn.mailbox > a"))
{
// Click at a lot of page and the page will reload eventually
}
The problem is, inside the loop, I need to change pages and stuff but at the end I get back to that page that has the link list.
As soon as I hit the second iteration, I get the following error :
Probably because I changed page and even thought the links in the collection I loop through are the same, the compiler doesn't seem to think it's the exact same collection.
Is there a way around this or a workaround I could use ?
The reason you're getting the exception is because the page the driver.FindElementsByCssSelector is referencing has been reloaded.
Something like this should work:
Create an array of link text. Iterate through the link text array, clicking each link.
string [] links = new string[driver.FindElementsByCssSelector("span.cn.mailbox > a").Count);
int i = 0;
foreach (IWebElement link in driver.FindElementsByCssSelector("span.cn.mailbox > a"))
{
links[i++] = link.Text;
}
for (int i = 0; i < driver.FindElementsByCssSelector("span.cn.mailbox > a").Count; i++)
{
driver.FindElementByLinkText(links[i]).Click();
}
Really really odd problem, in short, I'm doing a foreach over every word in a textblock, if that word starts with for example "#" I want to make a username hyperlink out of it. However in about 70% of the cases it replaces the text fine, but it just doesn't become a hyperlink.
Partial code:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
var kaas = Tweet.Split(' ');
foreach (string a in kaas)
{
if (a.StartsWith("#"))
{
Hyperlink uname = new Hyperlink();
uname.NavigateUri = new Uri("http://twitter.com/" + "xarinatan");
uname.RequestNavigate += new RequestNavigateEventHandler(Hyperlink_RequestNavigateEvent);
uname.Inlines.Add("ASDAS");
TweetBlock.Inlines.Add(uname);
//TweetBlock.Inlines.Add(Username(a));
TweetBlock.Inlines.Add(" ");
}
}
}
Above code turns all instances that start with "#" into "ASDAS" but fails most of the time to properly convert it to a hyperlink, HOWEVER it DOES convert it SOMETIMES.
It's completely beyond me how it only works sometimes, instead of all the time or not at all.
All suggestions are welcome!
edit: To clarify, it -always- replaces the text with 'ASDAS', but in 70% of the cases it doesn't become a hyperlink.
My friend found the answer. Somehow inlines being added as string causes sporadic behavior, they have to be added as a 'Run'.
Fix can be found here: https://github.com/zahndy/o3o/commit/68b50f8c0ea106bcc709d3f69658b28da9c8a9d4#diff-3
Thanks all for the suggestions!
We have a web browser in our Winforms app to nicely display history of a selected item rendered by xslt.
The xslt is writing out <a> tags in the outputted html to allow the webBrowser control to navigate to the selected history entry.
As we are not 'navigating' to the html in the strict web sense, rather setting the html by the DocumentText, I can't 'navigate' to desired anchors with a #AnchorName, as the webBrowser's Url is null (edit: actually on completion it is about:blank).
How can I dynamically navigate to Anchor tags in the html of the Web Browser control in this case?
EDIT:
Thanks sdolphion for the tip, this is the eventual code I used
void _history_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
_completed = true;
if (!string.IsNullOrEmpty(_requestedAnchor))
{
JumpToRequestedAnchor();
return;
}
}
private void JumpToRequestedAnchor()
{
HtmlElementCollection elements = _history.Document.GetElementsByTagName("A");
foreach (HtmlElement element in elements)
{
if (element.GetAttribute("Name") == _requestedAnchor)
{
element.ScrollIntoView(true);
return;
}
}
}
I am sure someone has a better way of doing this but here is what I used to accomplish this task.
HtmlElementCollection elements = this.webBrowser.Document.Body.All;
foreach(HtmlElement element in elements){
string nameAttribute = element.GetAttribute("Name");
if(!string.IsNullOrEmpty(nameAttribute) && nameAttribute == section){
element.ScrollIntoView(true);
break;
}
}
I know this question is old and has a great answer, but this hasn't been suggested yet, so it might be useful for others that come here looking for an answer.
Another way to do it is use the element id in the HTML.
<p id="section1">This is a test section</p>
Then you can use
HtmlElement sectionAnchor = webBrowserPreview.Document.GetElementById("section1");
if (sectionAnchor != null)
{
sectionAnchor.ScrollIntoView(true);
}
where webBrowserPreview is your WebBrowser control.
Alternatively, sectionAnchor.ScrollIntoView(false) will only bring the element on screen instead of aligning it with the top of the page