Finding element using Selenium Web Driver in C# - c#

So I've been working learning how to use Selenium in C# to do some automated testing for a project. However, I've hit a roadblock on this one. I have been trying to figure out of way to click the following link on this webpage.
Here is what I'm trying to target:
<A class='PortalLink' HREF="https://mywebsite.com/myprograms/launchprogram.jsp?" onClick="setUser('login','password');"><span>MyProgram</span></A>
Searching by ClassName hasn't turned up anything. Although there are multiples, I just wanted to see if I could detect the presence of them.
By.ClassName("PortalLink")
I tried a href based search using CssSelector, but this failed as well.
By.CssSelector("[href*='https://mywebsite.com/myprograms/launchprogram.jsp?']")
Lastly, I tried to use XPath and search by class and span content, but this failed to find the link as well.
By.XPath("//A[contains(#class,'PortalLink') and span[text()='MyProgram']]")))
The webpage in question contains 2 frames which I've tried both.
I'm waiting 200 seconds before timing out. What am I doing incorrectly? Thanks in advance for any help!

Assuming that this element is not appended to the DOM during ajax, your statement should be
By.CssSelector("a.PortalLink[href*='launchprogram.jsp']")
If there are multiple of these links, then we'll need to go further up in the parent-child hierarchy since this link has no more attributes that make this link unique.

If you can post the parent html of this link then we can suggest more options,
Can you try these......
//span[contains(text(),'MyProgram']
//span[contains(text(),'MyProgram']/../

Related

Selenium cant access any elements on the webpage

I'm writing Selenium tests in C# with Specflow and Nunit for a new project i'm working on and nothing inside the body can be selected anywhere at all
I can access body with
WaitUntil(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.CssSelector("body")));
but can not of the following work and throe element not found exceptions:
WaitUntil(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.CssSelector("halo-root")));
WaitUntil(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.CssSelector("halo-entry")));`
WaitUntil(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.CssSelector("halo-navigation-container")));
etc
error: no such element: Unable to locate element: {"method":"css selector","selector":"halo-root"}
There are no iframes
I have tried long waits to ensure its nothing asynchronous
the website is angular.js (I have tried some protractor approaches but nothing successfully)
I've also played around wit ElementIsVisible, ElementIsClickable etc
Ive tried different selectors (id, className, xpath etc)
All help appreciated
p.s cant share the entire SUT as it required vpn access.
First check your html. i think body tag must be present in iframe.
If it is present in iframe first switch to iframe then try any commands(wait or actions) on element.
The issue was when logging in through a portal (Jumpcloud) a new tab was opened (that I didn't notice) so the solution was just switch to that new window with _driver.SwitchTo().Window(_driver.WindowHandles[1])
Thanks for all the help though! :)

How to open multiple links in different windows using selenium and C#

I have been trying to open different subreddits using selenium, but I can't seem to figure it out. I want to be able to open a specified number of subreddits, from a specified search term.
Usually, I would just put in the url, but I can't do that, because when the user inputs a different keyword, it would come up with different results. Here's the example link.
When I use inspect on the first 3 subreddits (the ones I want to click), I can't really see a real way to differentiate them, other than the subreddit name (can't use, as people will be using different search terms).
Any help would be much appreciated!
Using Visual Studio, C#, and selenium
Can locate all such links present in page using a common locator and then click all of them one by one:
IList<IWebElement> reditLinks= driver.FindElements(By.XPath("//span[text()='Communities and users']//following-sibling::div//a//div//div[contains(#class,'_2torGbn')]"));
// For current page, it will return list of 3 elements.
for (WebElement reditLink: reditLinks){
reditLinks.Click();
}
Note : I am no expert in C#, I am more of a Java / Python person. But idea here will work.

Selenium: Finding if a Webelement is contained inside another Webelement?

I'm currently learning Selenium by building a test framework up to test Trello - for no good reason other than it's free and reasonably complex.
The problem I have right now is that, I want to check if a card is in a certain column or not.
I have the card & column as WebElements so what I'm doing right now is:
column.FindElements(By.LinkText(_card.Text)).Count > 0;
But this doesn't work if there's no text and seems pretty brittle.
What I want is something like:
column.Contains(_card)
I've searched on SO but I've only seen solutions which pass an XPath - I don't have an XPath, I have the WebElement.
Any ideas?
Two things,
relative xpath is fairly easy to learn and could probably take care of this for you.
CSS selectors should also easily identify the container regardless of the text. Without seeing the code, I can't help much more.
You should be able to find all elements with a certain css tag.
Using Firefox with the Firebug extension, right click your element and go to Inspect Element with Firebug. Then, when the html of your element comes up in the window, right click the element and select Copy XPath. Now you have an XPath to use.
To use the CSS Selectors that others are talking about, you can select Copy CSS Path instead of Copy XPath.
Hope this helps.

Selenium webdriver C# - Unable to find the element in a grid developed using angular UI

I am trying to automate a web application developed using angular JS through selenium webdriver(C#) and in that i am trying to click on a cell in a angular UI grid, i tried finding by css selector or xpath but it didn't help.
Css selector is generating dynamic ID - #\31 460691734316-0-uiGrid-00KQ-cell > div
Xpath is also dynamic //*[#id="1460691734316-0-uiGrid-00KQ-cell"]/div
and i tried using
driver.FindElements(By.CssSelector("*[id^='1460'][id$='cell']"));
but it didn't help
any help will be highly appreciated. I can send more details if needed
For my particular problem with the HTML page containing iframes and developed with AnglularJS the following trick saved me a lot of time: In the DOM I clearly saw that there is an iframe which wraps all the content. So following code supposed to work:
driver.switchTo().frame(0);
waitUntilVisibleByXPath("//h2[contains(text(), 'Creative chooser')]");
But it was not working and told me something like "Cannot switch to frame. Window was closed". Then I modified the code to:
driver.switchTo().defaultContent();
driver.switchTo().frame(0);
waitUntilVisibleByXPath("//h2[contains(text(), 'Creative chooser')]");
After this everything went smoothly. So evidently Angular was mangling something with iframes and just after loading the page when you expect that driver is focused on default content it was focused by some already removed by Angular frame. Hope this may help some of you.
What about trying to find the element with Selenium IDE which is a plugin of firefox ?
In the IDE, you can easily find the selector using selecting the element with GUI
Rather than identifying the element specifically by its ID, could you use the elements around it instead? Is this cell within a table and at a consistent position? Is there a parent element you could more consistently select and iterate through the children in your C# program to identify the appropriate cell you're looking for?

C# data scraping from websites

HI I am pretty new in C# sphere. Been in php and JavaScript since the beginning of this year. I want to scrap posts and comments from a blog. The site is http://www.somewhereinblog.net
What I want to do is
1. I want to log in using a software
2. Then download the html
3. Then use regular expressions, xpath whatever comes handy to separate the contents of posts and comments
I been searching all over. Understood very little. Though I am quite sure I need to use 'htmlagilitypack'. I dont know how to add a library to c# console or form application. Can someone give me some help? I badly need this. And I am not too into C# just a week. So would be grateful if there is some detailed information. Waiting eagerly.
Thanks in advance brothers.
Using Webclient you can login and download
Instead html-agility-pack I like CsQuery because lets you use jQuery syntax inside a string in C# code, so you can download to a string the html, and search and do things in it like with jQuery and HTML page.

Categories

Resources