In my previous posts what we have learn is we can Locate Element By Name, Locate Element By ID, Locate Element By Class Name And Locate Element By Tag Name to take any action on that element of software web application. We will look about different Selenium WebDriver software testing tool's actions and operations in my upcoming posts. Now let us learn two more element locators 1. Locate Element By Link Text and 2. Locate Element By Partial Link Text on software web application page.
Locate Element By Link Text
If your targeted element is link text then you can use by link text element locator to locate that element on software web application page. Look in to bellow given image.
My Targeted element Is Link text 'Click Here' and I wants to click on It then I can use It as bellow.
driver.findElement(By.linkText("Click Here")).click();
Locate Element By Partial Link Text
Same as Link text, We can locate element by partial link text too on software web application page. In that case we need to use By.partialLinkText at place of By.linkText as bellow.
driver.findElement(By.partialLinkText("Click ")).click();
For locating element by link text on software web application page, we need to use full word 'Click Here' but in case of locating element by partial link text, we can locate element by partial work 'Click' too.
Look in to bellow given example. I have used both the element locators to locate links.
Copy bellow given @Test method part and replace it with the @Test method part of example given on this page. (Note : @Test method is marked with pink color in that example).
@Test
public void test()
{
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.linkText("Click Here")).click();//Locate element by linkText and then click on it.
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("18:"))); //Locate element by partial linkText.
}
please explain last two lines of code
ReplyDeleteWebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("18:"))); //Locate element by partial linkText.
}
Last two lines will wait for link to become clickable.
DeleteWebDriverWait wait = new WebDriverWait(driver, 15);
ReplyDeleteIn the above code is 15 time give to wait in milliseconds??
It Is In second
Deletei need to cick a link but that name is available in many place. but i when i try to locate it by path , everytime the path is getting failed.
ReplyDeleteany other key word to click that link .
example , i have link "new" located in two different place of a table
you can try with dynamic xpath and dynamic css selector, so you can find it easily
Delete-^6years experiance in selenium.
If the link cannot be identified uniquely, then you need to look for other locators or XPATH / CSS should solve this.
ReplyDeleteCould you explain this code?
ReplyDelete//wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Đăng ký bằng email"))); //Locate element by partial linkText.
}
What this function to capture?
Thanks,
browser.get(url)
ReplyDeletei = 0;
try:
while (True):
print (i)
i = i+1
link = browser.find_element_by_link_text('View more comments')
link.click()
except Exception as e:
print (e)
pass
how can limit to one post