Could get the link in selenium using href attribute - selenium

I tried following to get the link on Start Onboarding but could not.
//string webElement = driver.FindElement(By.XPath("/html/body/div/div/div/a")).GetAttribute("href");
var tes = driver.FindElement(By.XPath(#"/html/body/div/div/div/a")).GetAttribute("href");
var web2 = driver.FindElement(By.XPath(#"//a[contains('saa',#href)"));
var web3 = driver.FindElement(By.XPath(#"//a[text()='saa')]/#href"));
var tes2 = driver.FindElement(By.XPath(#"//a[text()='saa')]/#href")).GetAttribute("href");
Start Onboarding
Please suggest me, where I fail and what I can do. I am new to selenium.
Thanks in advance.

A better way is to stay away from xpaths as they are rigid and do not work across all browsers. The below would be better.
var tes2 = driver.FindElement(By.cssSelector("a#idOfElement")).getAttribute("href");
or
var tes2 = driver.FindElement(By.cssSelector("a.classnameOfElement")).getAttribute("href");

Related

phantomjs to render a page from a string

I want to render a webpage from a string. I've looked at the docs of phantomjs and they suggested the following:
var webPage = require('webpage');
var page = webPage.create();
var expectedContent = '<html><body><div>Test div</div></body></html>';
var expectedLocation = 'http://www.phantomjs.org/';
page.setContent(expectedContent, expectedLocation);
It's not quite working. Why? (I use the latest version).
I suggest you render a normal page (about:blank works) and then do webPage.content='<html><body><div>Test div</div></body></html>';
then render your page.
hope that helps.

Selenium SendKeys to JqueryUi Popup

I have been having troubles with Selenium on and off for quite a while now. So, I thought I would create a nice simple example and hopefully a selenium expert can show me:
what I am doing wrong; or
what is wrong with the website that I am driving.
For this exercise, I have created a simple console application with a view to logging in to a library website. Obviously, username and password are not correct. But the fact is, the code which I present below cannot even type characters in a text box which is visible on the page:
static void Main(string[] args)
{
ChromeDriverService chromeDriverService = ChromeDriverService.CreateDefaultService(#"E:\");
var chromeOptions = new ChromeOptions();
chromeDriverService.Port = 7788;
var driver = new ChromeDriver(chromeDriverService, chromeOptions);
var nav = driver.Navigate();
nav.GoToUrl("http://sapln.ent.sirsidynix.net.au/client/charlessturt/");
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementExists(By.CssSelector("#libInfoContainer > div.loginLink > a"))).Click();
wait.Until(ExpectedConditions.ElementExists(By.Id("j_username"))).SendKeys("gfhjdskaf");
wait.Until(ExpectedConditions.ElementExists(By.Id("j_password"))).SendKeys("gfhdsjffd");
driver.FindElementById("submit_0").Click();
Console.ReadKey();
}
By all means, try and populate that text box. I would love to hear how you achieve it.
I am using:
v.2.43.1 of Selenium with the latest version of Chrome
v.2.43 of ChromeDriver
your dialog is opening in an iframe. You'll need to switch to that iframe before you can interact with the login form elements.
I didn't test your code, but you can follow the example below, by removing my comments:
static void Main(string[] args)
{
ChromeDriverService chromeDriverService = ChromeDriverService.CreateDefaultService(#"E:\");
var chromeOptions = new ChromeOptions();
chromeDriverService.Port = 7788;
var driver = new ChromeDriver(chromeDriverService, chromeOptions);
var nav = driver.Navigate();
nav.GoToUrl("http://sapln.ent.sirsidynix.net.au/client/charlessturt/");
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementExists(By.CssSelector("#libInfoContainer > div.loginLink > a"))).Click();
assuming your iframe is the first found you can use something like...:
driver.switchTo().frame(driver.findElements(By.tagName("iframe").get(0)));
Since your iframe doesn't have an id, we can't simply do:
driver.switchTo().frame("id of frame");
It would be super convenient if you could give your iframe an id.
Now lets continue with your code:
wait.Until(ExpectedConditions.ElementExists(By.Id("j_username"))).SendKeys("gfhjdskaf");
wait.Until(ExpectedConditions.ElementExists(By.Id("j_password"))).SendKeys("gfhdsjffd");
driver.FindElementById("submit_0").Click();
Once you're logged in, you need to switch back to the default content
driver.switchTo().defaultContent();
Console.ReadKey();
}
Simply inject that iframe switch before you attempt to interact with dialog, and inject the defaultContent() switch at the end before attempting to do anything else with initial page and you're good.
Good luck!

Is there any command for page to load completely in Jmeter Webdriver

var pkg = JavaImporter(org.openqa.selenium)
var support_ui = JavaImporter(org.openqa.selenium.support.ui.WebDriverWait)
var wait = new support_ui.WebDriverWait(WDS.browser, 5000)
var url = WDS.args[0];
var user = WDS.args[1];
var pwd = WDS.args[2];
WDS.sampleResult.sampleStart()
WDS.browser.get(url)
var wait=new support_ui.WebDriverWait(WDS.browser,15000)
var userName = WDS.browser.findElement(pkg.By.id('Login_txtUserName')).sendKeys([user])
//userName.click()
//userName.sendKeys(['pandian'])
var userPwd = WDS.browser.findElement(pkg.By.id('Login_txtPassword')).sendKeys([pwd])
//userPwd.click()
//userPwd.sendKeys(['1234'])
var button = WDS.browser.findElement(pkg.By.id('Login_btnLogin')).click()
if i execute the above code it's working fine, but what happen is when the browser is open the page is not loading completely it's just navigating to another element, so i want the page to load completely, "Is there any command available for JMETER" bcoz the command in jmeter differs from selenium webdriver....
Please provide the command
Yes, you can add a "wait for element" webdriver command to wait until a certain element is viewable / clickable etc. Using the wait.until command.
Here is an example:
var pkg = JavaImporter(org.openqa.selenium, org.openqa.selenium.support.ui)
var wait = new pkg.WebDriverWait(WDS.browser, 5000)
WDS.sampleResult.sampleStart()
WDS.browser.get("http://www.somesite.com/browse/product.php?pid=12345");
WDS.browser.findElement(pkg.By.id("addToBagBtn")).click();
wait.until(pkg.ExpectedConditions.elementToBeClickable(pkg.By.id('inlineBagCheckOut2')))
WDS.sampleResult.sampleEnd()

How to get multi page JMeter Webdriver timing

I've been using JMeter for quite a while but webdriver is new to me.
I'm trying to do some timings for a multi-page scenario and have a question.
I'm using JMeter webdriver sampler and HTMLunit:
Here is the scenario
1. Go to a web page http://162.243.100.234
2. Enter the word hello in the search box
3. Click on submit
What I want to get is:
1. How long it took to load the first page
2. How long it took from when I clicked on submit to when the results page was loaded
I have the following code which only gives me ONE sample timing.
How do I change it so I'll have two?
var pkg = JavaImporter(org.openqa.selenium)
var support_ui = JavaImporter(org.openqa.selenium.support.ui.WebDriverWait)
var wait = new support_ui.WebDriverWait(WDS.browser, 5000)
WDS.sampleResult.sampleStart()
WDS.browser.get('http://162.243.100.234/')
var searchField = WDS.browser.findElement(pkg.By.id('s'))
searchField.click()
searchField.sendKeys(['hello'])
var button = WDS.browser.findElement(pkg.By.id('searchsubmit'))
button.click()
WDS.sampleResult.sampleEnd()
I tried adding another sampleStart and sampleEnd but got and error.
Do I need to use two samplers somehow?
Yep, you need to split your code into 2 pieces:
First Sampler:
WDS.sampleResult.sampleStart()
WDS.browser.get('http://162.243.100.234')
WDS.sampleResult.sampleEnd()
Second Sampler:
var pkg = JavaImporter(org.openqa.selenium)
WDS.sampleResult.sampleStart()
var searchField = WDS.browser.findElement(pkg.By.id('s'))
searchField.click()
searchField.sendKeys(['hello'])
var button = WDS.browser.findElement(pkg.By.id('searchsubmit'))
button.click()
WDS.sampleResult.sampleEnd()
Mention WDS.sampleResult.sampleStart() and WDS.sampleResult.sampleEnd() methods invocation
As per Using Selenium with JMeter's WebDriver Sampler guide
WDS.sampleResult.sampleStart() and WDS.sampleResult.sampleEnd()
captures sampler’s time and will track it. You can remove them, the
script will still work but you can’t get load time
Hope this helps

How to verify links

How to verify whether links are present or not?
eg.
I have 10 links in a page, I want to verify the particular link
Is it possible?
I am using selenium with Java.
Does i can write inside the selenium code
eg
selenium.click("searchimage-size");
selenium.waitForPopUp("dataitem", "3000");
selenium.selectWindow("name=dataitem");
foreach(var link in getMyLinkTextsToTest())
{
var elementToTest = driver.findElement(By.linkText(link));
Assert.IsNotNull(elementToTest);
}
What you can do is find all links on the page like this:
var anchorTags driver.findElement(By.TagName("a"));
and then iterate through the anchorTags collection to make you you've got what you're looking for.
Or if you have a list of the link texts you can do something like this:
foreach(var link in getMyLinkTextsToTest())
{
var elementToTest = driver.findElement(By.linkText(link));
Assert.IsNotNull(elementToTest);
}
This code is all untested and right off the top of my head so you might need to do some slight modification but it should be close to usable.
if you are using Selenium 1.x you can use this code.
String xpath = "//<xpath till your anchor tag>a/#herf";
String href = selenium.getAttribute(xpath);
String expectedLink = "your link";
assertEquals(href,expectedLink);
I hope this may help you...
List<WebElement> links = driver.findElements(By.tagName("a"));
for(WebElement we : links) {
if("Specific link text".equals(we.getText("Specific link text"))) {
we.click();
}
}
I'm taking all links to List variable 'links' and iterating it. Then checking condition, for the specific text we looking in the link is presenting in the list or not. If it found out, it'll click on it
If you're looking to verify each specific for the content of href, you can use javascript to return the outerHTML for a specific Webelement which you can identify however you like; in the example below I use By.cssSelector:
WebElement Element = driver.findElement(By.cssSelector("..."));
String sourceContents = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].outerHTML;", element);
assertEquals(sourceContents, "Learn More");
If you want to make it a tad more elegant you can shave the undesired elements off of the string, but this is the general case as of Selenium-java: 2.53.1 / Selenium-api: 2.47.1 as I can observe.
Best approach would be to use getText() method
List<WebElement> allLinks = driver.findElements(By.tagName("a"));
for(WebElement specificlink : allLinks ) {
if(specificlink.getText().equals("link Text"){
//SOPL("Link found");
break;
}
}