It is necessary to determine the level of nesting of pages in clicks from home page. How to do it right? I understand that all the pages from the site will get recursively.
The code will look like this:
public void getPageLinks(String URL) {
//4. Check if you have already crawled the URLs
//(we are intentionally not checking for duplicate content in this example)
if (!links.contains(URL)) {
try {
//4. (i) If not add it to the index
if (links.add(URL)) {
System.out.println(URL);
}
//2. Fetch the HTML code
Document document = Jsoup.connect(URL).get();
//3. Parse the HTML to extract links to other URLs
Elements linksOnPage = document.select("a[href]");
//5. For each extracted URL... go back to Step 4.
for (Element page : linksOnPage) {
getPageLinks(page.attr("abs:href"));
}
} catch (IOException e) {
System.err.println("For '" + URL + "': " + e.getMessage());
}
}
}
Only still there will be a check whether the link is a link to an external site, if so then you do not need to go to it.
am trying to click on this game to open it's page but every time gives me null pointer exception whatever locator am using still gives me same error also i tried to do a select from list as the link seems to be inside an "li" but didnt work also.
anyone could help me with the code to click this item ??
Targeted Page Url:
https://staging-kw.games.getmo.com:/game/43321031
Search(testCase);
WebElement ResultList = driver.findElement(By.xpath(testData.getParam("ResultList")));
log.info("list located ..");
List<WebElement> Results = ResultList.findElements(By.tagName(testData.getParam("ResultListItems")));
for (WebElement List : Results) {
String Link = List.getAttribute("href");
try {
if (Link.equals(null)) {
log.info("null");
}
if (Link.equals(testData.getParam("GameLink")) && !Link.equals("") && !Link.equals(null)) {
List.click();
}
} catch (NullPointerException countryIsNull) {
log.info("LinkIsNull");
}
}
//clickLink(GameLocator,driver);
}`
I got this code to work by just adding this line after the search method
driver.findElement(By.cssSelector("li.title")).click();
and how did i get it ? .. i used Selenium IDE to record the actions then converted the code to Java -TestNG to get the exact web element selector
I'm using Selenium to log into my Google account and to visit YouTube.
Now on the video manager I would like to get all of my video ids. I tried copying the CSSSelector or XPath which the developer tools in Chrome give me but each of them contain the video id which makes them impossible to use like this:
List<WebElement> allVideoUrls = driver.findElements(By.cssSelector("my-selector-which-gives-all-videos-on-page"));
Note that I have to be logged in to be able to "see" unlisted or private videos as well so that's required.
So far I have a bad implementation which sometimes fails to work for some reason. I firstly get all the links on the page and only return the ones which are for editing a video. To avoid a StaleElementReferenceException I'm retrieving all links again inside the loop.
public void getVideoInformation()
{
// Visit video manager
driver.get("https://www.youtube.com/my_videos?o=U");
// Wait until video list has loaded
new WebDriverWait(driver, 10).until(ExpectedConditions
.visibilityOfElementLocated(By
.cssSelector("#vm-playlist-video-list-ol")));
// Return all links on page
List<WebElement> allLinks = driver.findElements(By.tagName("a"));
HashSet<String> videoLinks = new HashSet<>();
for (int linksIndex = 0; linksIndex < allLinks.size(); linksIndex++)
{
String link = driver.findElements(By.tagName("a")).get(linksIndex)
.getAttribute("href");
try
{
if (link.contains("edit"))
{
System.out.println(link);
// No duplicates
videoLinks.add(link);
}
} catch (Exception error)
{
error.printStackTrace();
}
}
// ...
}
I'm fine with the fact that I need to load every other page as well to get all the videos but please help me to find an efficient/reliable way of getting the video ids.
I am trying to get the value from UI, using (By.id) locator.getAtrribute("value"),
Issue:
But, when the xpath is not found (web element not present on UI), I need to take the value as '00:00', but I get error as "No Such element"
I need to add a check, to find the webelement is present on UI, if true, get value using getattribute("value") , else return the value as '00:00'
But When using If condition, "(WebelementforUIvalue.isEmpty()" returns '00:00', though the Web element is present on UI. (I need to take 00:00, when there is no element found/present on UI)
String WebelementforUIvalue = driver.findElement(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket)).getAttribute("value");
if (WebelementforUIvalue.isEmpty()) {
UIValue = "00.00";
} else {
UIValue = WebelementforUIvalue;
}
System.out.println(UIValue);
Personally I'd use a try/catch block for this particular case.
try {
String WebelementforUIvalue = driver.findElement(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket)).getAttribute("value");
UIValue = WebelementforUIvalue;
}
catch (NoSuchElementException) {
UIValue ="00.00";
}
Try like this instead(Java Code). :
try{
WebElement element = new WebDriverWait(driver,20).until(ExpectedConditions.presenceOfElementLocated(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket));
UIValue = element.getAttribute("value");
}catch(Throwable e){
UIValue = "00.00";
}
System.out.println(UIValue);
I have given an explicit timeout of 20 seconds. So, selenium will try to detect the presence of element in 20 seconds. If it doesn't find then it will timeout and send the assign "UIValue" to "00.00" or else if it finds the element it will assign the "value" attribute's content to "UIValue".
you can try it out this way...
String WebelementforUIvalue = null;
try{
WebelementforUIvalue = driver.findElement(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket)).getAttribute("value");
} catch(NoSuchElementException nsee) {
WebelementforUIvalue = "0.0";
}
System.out.println(WebelementforUIvalue);
You can handle this in better way using "findElements"(observer 's' at the end).
findElements returns the list of elements matching the locating criteria.
List<WebElements> list=driver.findElements(By.id("Your criteria"))
Integer intNoValues=list.size();
if(intNoValues=0)
{
UIValue = "00.00";
}
else
{
UIValue = WebelementforUIvalue;
}
If you want a detailed explanation of findElements, you can watch the below video
How to find element which doesnt exist using WebDriver
java, Webdriver, So now i have this, but the casting in the third line is giving me errors
public void getLinks(String linkName)throws Exception{
try {
List<WebElement> links = ((Webelement)driver).findElements(By.tagName("a"));
for (WebElement myElement : links){
String link = myElement.getText();
if (link.equals(linkName)){
myElement.click();
}
}
}catch (Exception e){
System.out.println("Error the link was not found "+e);
}
}
and if i run it this is what i see: Starting ChromeDriver (v2.6.232923) on port 34733
Error the link was not found java.lang.ClassCastException: java.lang.ThreadLocal cannot be cast to org.openqa.selenium.WebElement
PASSED: testing
Also my second idea is that it is not finding: “tagName("a"));” for links. But then when I go to the web I see Contacts in Portugal covering Airlines
So the links are in “a” tags, so I don’t think is this.
And im passing this : String linkName= "Contacts in Portugal covering Airlines";
I think this is a wrong assertion:
if (assertEquals(linkName, myElement)){
myElement.click();
}
.. because linkName is a String, myElement is a WebElement and the assertEquals() return value is not boolean (only void). You can check it with String equals(). For example:
if (link.equals(linkName)){
myElement.click();
}