Log in
updated 11:29 AM UTC, May 4, 2016

How to find a link element using selenium

Here is a small code snippet to find link element using complete name or partial name

 

	public WebElement link(String label) {
		WebElement we = this.we.findElement(By.linkText(label));
		return we;
	}

	public WebElement linkPartial(String label) {
		WebElement we = this.we.findElement(By.partialLinkText(label));
		return we;
	}

this.we = this.driver.findElement(By.tagName("body"))

Add a comment (0)

  • Written by Madhu V Rao
  • Category: Automation
  • Hits: 935

How to find a button/input using its name on a webpage using selenium

here is a small code snippet to find the button by its name

 

public WebElement button(String label) {
		By xpathForButton = By
				.xpath("//input[@type='submit' and @value='" + label + "']|//button[@type='submit' and normalize-space()='" +label+"']");

		List<WebElement> eles = this.we.findElements(xpathForButton);
		WebElement returnEle = null;
		for(WebElement ele: eles ) {
			if(ele.isDisplayed()) {
				returnEle = ele;
				break;
			}
		}
		if(Config.DEBUG) {
			System.out.println("Webelement found: "+ (we !=null? returnEle.getTagName() : we) );
		}
		return returnEle;
	}

this.we = this.driver.findElement(By.tagName("body"))

Add a comment (0)

  • Written by Madhu V Rao
  • Category: Automation
  • Hits: 899

how to wait in selenium

This code snippet has the selenium webdriver (java) code to wait till certain condition occurs. In this case it is waiting till the 'body' tag is present.

 

WebDriverWait wait = new WebDriverWait(driver, 10);
		wait.until(new ExpectedCondition<Boolean>() {
			public Boolean apply(WebDriver wdriver) {
				return driver.findElement(By.tagName("body")) != null;
			}
		});

Add a comment (0)

  • Written by Madhu V Rao
  • Category: Automation
  • Hits: 930

How to take a screenshot of the website using selenium webdriver for java

This code snippet shows how to take a screenshot in selenium java webdriver

 

public void take_a_copy_of_screenshot(String fileName) throws IOException {
		File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
		FileUtils.copyFile(screenshot, new File(YOUR_FOLDER + "\\" + YOURFILENAME + ".png"));	
	}

TakeScreenshot class is from selenium

import org.openqa.selenium.TakesScreenshot;

Add a comment (0)

  • Written by Madhu V Rao
  • Category: Automation
  • Hits: 908

Finding webelement by its associated label using Selenium

Find a HTML element (input/select or textarea) using the associated label using Selenium

	public WebElement byLabel(int occurance,String label) {
		if(occurance <=0) occurance = 1;
		WebElement first = null;
		int position = 1;
		List<WebElement> wes = we.findElements(By
				.xpath("//label[normalize-space()='" + label + "']"));
		for (WebElement we : wes) {
			if (we.isDisplayed()) {
				if (position == occurance) {
					if(Config.DEBUG) {
						System.out.println("[DEBUG] Found " + occurance + " element by label: " + label);
					}
					first = we;
					break;
				} else {
					position++;
				}

			}
		}
		if (first == null)
			throw new IllegalArgumentException("no label with text=" + label);
		String f = first.getAttribute("for");
		if (f == null)
			throw new IllegalArgumentException("for == null");
		WebElement we2 = we.findElement(By.id(f));
		if (we2 == null)
			throw new IllegalArgumentException("no element for id: " + f);
		return we2;
	}

Add a comment (0)

  • Written by Madhu V Rao
  • Category: Automation
  • Hits: 1088
Follow Us on Twitter
Find Us on Facebook
Follow Us on Google
Follow Us on Pinterest