WebCrawler click on a link not working when minimized - c#

I have a crawler for this link:
http://www.ncbi.nlm.nih.gov/pubmed?term=Breast%20cancer%5BTitle%2FAbstract%5D
Unfortunately the links are handled by javascripts and there is no Href.
for this purpose I've created a crawler with a Web Browser component. I used this code to click on the link:
webBrowser1.Document.GetElementById("EntrezSystem2.PEntrez.PubMed.Pubmed_ResultsPanel.Entrez_Pager.Page").Parent.Children[3].Focus();
SendKeys.Send("{enter}");
but the problem is when I minimize the application, It can not click on the link because it can not focus. what should I do?

You can try to do this with JS:
var id = "EntrezSystem2.PEntrez.PubMed.Pubmed_ResultsPanel.Entrez_Pager.Page"
var script = "document.getElementById('{0}').parentNode.children[3].click()"
script = string.Format(script, id);
webBrowser1.Document.InvokeScript("eval", new object [] { script })
Additionally, I would advice to embed jQuery for better DOM navigation.

Related

How to assign HtmlDocument to a web browser, then interact with specific controls via GetElementById in c# / Console application

I have a requirement to navigate to a specific website, select a value from a drop down list, then click on a submit button. The solution has to be via a console app via c#.
So working backwards, I need to eventually be able to interact with controls like this:
html.GetElementById("shellmenu_0").InvokeMember("Click");
In order to work with html elements I need an HtmlDocument object. So as an example, the code below attempts to go to the Microsoft site and click a link to a new page. The code is as follows.
WebBrowser wb = new WebBrowser();
wb.AllowNavigation = true;
wb.Navigate("https://www.microsoft.com/en-us/?ql=4");
HtmlDocument document = wb.Document;
document.GetElementById("shellmenu_0").InvokeMember("Click");
I think I am on the right track, but I am hitting a null exception on the document. I must be missing a step to assign the website to the document somehow.
Any advice would be greatly appreciated. Thanks in advance!

Failed to locate element bind to an iframe

I am trying to automate login of following site:
https://www.sbs.com.au/ondemand/
after pressing Signin/Signup its opening login modal which is an iframe.
I am trying to locate email,password, login button with following code but not working. Its opening modal but after that doing nothing just test failing but no error message either.
Code for clicking Signin/signup link and switch to iframe:
var link = Driver.Instance.FindElement(By.Id("capture_signin_link"));
link.Click();
Driver.Instance.Manage().Timeouts().ImplicitWait.Seconds.Equals(10);
Driver.Instance.SwitchTo().Frame(1);
Code for passing value in email,password,and clicking login button:
var inputus = Driver.Instance.FindElement(By.Name("email"));
inputus.SendKeys(username);
Driver.Instance.Manage().Timeouts().ImplicitWait.Seconds.Equals(5);
var inputpass = Driver.Instance.FindElement(By.Name("password"));
inputpass.SendKeys(password);
Driver.Instance.Manage().Timeouts().ImplicitWait.Seconds.Equals(5);
var loginbutton = Driver.Instance.FindElement(By.ClassName("traditional-login"));
loginbutton.Click();
I am not sure if I am passing correct value to find locators and ifame. Please visit above site information for DOM info.
Looking forward a good solution. Please help me
My Latest Code for clicking signup to open login modal:
var link = Driver.Instance.FindElement(By.Id("capture_signin_link"));
link.Click();
Driver.Instance.Manage().Timeouts().ImplicitWait=TimeSpan.FromSeconds(10.00);
Driver.Instance.SwitchTo().Frame(Driver.Instance.FindElement(By.Id("login-iframe")));
Driver.Instance.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10.00);
Code for login:
IWebElement inputus = Driver.Instance.FindElement(By.XPath(".//*[#id='onboarding']/div[2]/form/section[2]/div/div[1]/input"));
inputus.SendKeys(username);
Driver.Instance.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10.00);
var inputpass = Driver.Instance.FindElement(By.Name("password"));
inputpass.SendKeys(password);
Driver.Instance.Manage().Timeouts().ImplicitWait=TimeSpan.FromSeconds(10.00);
var loginbutton = Driver.Instance.FindElement(By.ClassName("traditional-login"));
loginbutton.Click();
Above code worked for Chrome. On firefox its showing "Array out of index error" on Sendkeys() method. I found on some blog a lot of people are facing same problem and suggesting to downgrade firefox browser version to get proper support from getco driver as getco driver failing to handle sendkeys().
I haven't try above suggestion. Will try and update here. Till then if you have any other suggestion on that plz let me know.

Save current window URL and move back to that URL after couple of navigation

I have a site and in my site when user click on a specific div then a new child window open . Suppose the user is standing at xyz.com/category , now if you user clicks on that div then parent windows goes to another url suppose google.com and a popup window will get open and there are some more navigation in popup windows . But every page that will open in popup has a button called Go Back To Site . I want that when user clicks on several links in popup and then click on Go Back To Site button then popup window get closed and parent window move back to xyz.com/category.
You can try PHP Superglobal array
$_SERVER['HTTP_REFERER']
If you can use javascript for this, there are simple functions such as:
window.history.back();
window.history.forward();
You can refer all of them in following link:
https://developer.mozilla.org/en/docs/DOM/Manipulating_the_browser_history
For Jquery:
$(document).ready(function() {
var referrer = document.referrer;
});
For C# :
WebBrowser1.Url = new Uri("http://maps.google.com");
- document.referrer does not work for IE
For this issue refer following code:
<script type="text/javascript" >
function redirect(url) {
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
var referLink = document.createElement('a');
referLink.href = url;
document.body.appendChild(referLink);
referLink.click();
} else {
location.href = url;
}
}
</script>
This code can also be refered over here
The way I'd do it is to pass the current URL as a parameter into your popup. So you would change your anchor link tags to this:
Click
So when your popup pops up, you just grab the value of prevPage and then when they click "Back to previous page", you just redirect to the value of prevPage.

How do I fire a aspx script when a dynamically added button is clicked?

Here is the context:
I am building a .aspx page that allows the user to administrate some xml documents we have on our server. The page content is loaded using AJAX, so buttons and forms are dynamically added to the document.
If I had static buttons that I was creating within the .aspx page before it loads on the client's machine, I could attach an event to it very easily. However, I'm dynamically adding and removing buttons and forms on the fly, using jQuery.
Here is a simplified example:
In the following jsFiddle, I'm pretending that the html document contains the following script:
<script language="C#" type="text/C#" runat="server">
void SaveAllChanges(Object sender, EventArgs e)
{
Button clickedButton = (Button)sender;
clickedButton.Text = "foobar";
}
</script>
And that I have a javascript file that contains the following:
$('button.buttonGenerator').click(function() {
$('.buttonContainer').append(
'<button onclick="SaveAllChanges">' +
'Save All Changes!' +
'</button>'
);
});
Obviously the buttons I am creating can not run the function SaveAllChanges with the way it is now. I added the onclick attribute to show what I needed to happen, in a pseudo-code kind of style.
How can I make it so that dynamically added buttons can run the C# method I have defined within the script tag at the top of the document?
Here is the jsfiddle: http://jsfiddle.net/2XwRJ/
Thanks.
You can give all buttons that must save changes a common class (e.g. class="ajaxButton") and have one jQuery method that responds to click events on elements matching that class (use live so that updates to the DOM are reflected).
$("button.ajaxButton").live("click", function(){
// Perform your Ajax callback to run server-side code
});
What you need to do is use something like ..
$(document).ready(function() {
$('button.buttonGenerator').click(function() {
$('.buttonContainer').append(
'<button id="#dynamicCommentButton" onclick="SaveAllChanges">' +
'Save All Changes!' +
'</button>'
);
});
$(document).on('click', '#dynamicCommentButton', function() {
alert($(this).attr('id'));
});
});
You are not going to be able to add the buttons like you have it there as this code here is just adding it as an HTML DOM element and the onclick attribute will be the on the client element. As a result clicking the button will try fire a SaveAllChanges javascript function
$('.buttonContainer').append(
'<button onclick="SaveAllChanges">' +
'Save All Changes!' +
'</button>'
);
What would be best would be to create that SaveAllChanges function in javascript and then you can handle it from there. Two of the ways I see you being able to do this are:
Have a http endpoint setup (script service, web api or just posting to a page) that you call using Ajax from your javascript. You can then pass through any needed arguments.
You could have a hidden element and hidden button on the page so that when the javascript is called it populates any arguments you need and then clicks the hidden button and posts the page back.
Personally I would choose the first approach from a user experience stand point as the page will not be posting back each time. I have used something similar to the second approach and it works fine but just feels very clunky.

Executing client-side script on link click

I need my web page to open a window and enable a disabled button when a link or a button is clicked.
From what I've read on other posts on here, if I try and open a new window in Page_load most browsers will assume it's a pop-up and block it so I've been trying to do it client side with JS.
Currently, I'm trying it with a link declared like so:
Please click here to open the document.
This calls the following JS:
function OpenDoc()
{
<%= btnSubmit.ClientID %>.Visible = true;
Window.Open('GetDocument.aspx')
}
Unfortunately, instead of rending the JS as "btnSubmit.Visible = true" it comes out as "MainContent_btnSubmit.Visible = true" which doesn't work.
Assuming this is the best way of doing what I want, where am I going wrong?
You can't change visibility property through javascript but you can use the following code instead of it :
var control = document.getElementById('<%=btnSubmit.ClientID %>');
control.disabled = true;
In this case the button will be disabled and if you need to hide button not disableing it then use the following code :
var control = document.getElementById('<%=btnSubmit.ClientID %>');
control.style.display= "none";
Hope this is helpfull based on my understanding to your problem

Categories

Resources