Selenium drill week 3

Using Tags to access web elements

This challenge is for all the Automation and Selenium testers who want to improve their skill and knowledge every day.

Challenge:

The challenge is to launch Techcanvass website (http://techcanvass.com/Courses/business-analyst-training.aspx) and then click on any topic of Course Syllabus of ‘BA Certification Training’.

Please Note: You cannot use topic text to identify and click the web element.

Solution

If we had to click on any topic of Course Syllabus of ‘BA Certification Training’ manually then we would had to scroll down the page till the Course Syllabus section and then click on any topic.

Similarly working with Selenium WebDriver, we have to scroll the page to the Course Syllabus section and then click on any topic of our choice.

 But what if the Tags and other Meta elements of every web element are same? How to click the web element since we cannot use the text of the topic to identify the web element?

So in order to click the web element we have to use the close brackets after the tag and give the number of which web element we have to click.

For example, if we want to click the 8th web element then the code should be,

driver.findElement(By.xpath(<xpath>//<tag>[8]”))

So if we want to click the 2th web element then the code should be,

driver.findElement(By.xpath(<xpath>//<tag>[2]”))

Code:

public class MultipleButtons {

                public static void main(String[] args) throws InterruptedException {

                                System.setProperty(“webdriver.chrome.driver”, “<Path>”);

                                WebDriver driver = new ChromeDriver();

                                driver.manage().window().maximize();

                                WebDriverWait wait = new WebDriverWait(driver,120);

                                driver.get(“http://techcanvass.com/Courses/business-analyst-training.aspx”);

                                wait.until(ExpectedConditions.elementToBeClickable(By.linkText(“Selenium Certifications”)));

                                ((JavascriptExecutor)driver).executeScript(“arguments[0].scrollIntoView();”,driver.findElement(By.id(“Div3”)));

                                Thread.sleep(2000);

                                driver.findElement(By.xpath(“//div[@id=’Div3′]//following-sibling::div[@class=’ziehharmonika’]//h3[5]”)).click();

                                Thread.sleep(3000);

                                driver.close();

                }

}

The above code will launch Techcanvass website (http://techcanvass.com/Courses/business-analyst-training.aspx) in Chrome browser. Later it would scroll the page to Course Syllabus section and then click on the topic at 5th position and close the driver.


About Techcanvass

Techcanvass is an IIBA endorsed education provider (EEP), iSQI ATP (for Certified Agile Business Analyst Training) as well as Agile Testing alliance partner for CP-SAT certification training in Selenium.

Techcanvass offers wide range of automation testing courses. These courses include Java and Selenium certification course, Python Selenium Certification course, BDD Training with Cucumber, RPA Training with UiPath, DevOps certification course and more automation testing courses.

Selenium Certification Training