Getting value with Selenium

I was searching quite a lot for finding a solution for my problem and tried a lot with related solutions but they all don´t work for me so i guess i´m missing smth. and would be glad if someone would help me out. So maybe my code first:

    package Test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.server.handler.FindElement;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class oeffnen {

    WebDriver driver;

    public static void main(String[] args) throws FileNotFoundException {
        System.setProperty("webdriver.gecko.driver", "C:\\\\Program Files\\Java\\jdk-11.0.2\\bin\\geckodriver.exe");
            WebDriver driver = new FirefoxDriver();
            driver.get("https://www.bet365.com/#/IP/");
            driver.findElement(By.className("lpdgl")).click();
            driver.get("https://www.bet365.com/#/IP/");
            driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
            String value = driver.findElement(By.cssSelector("div.ipo-Competition:nth-child(1) > div:nth-child(3) > div:nth-child(2) > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > span:nth-child(2)")).getText();
            PrintWriter writer = new PrintWriter("D:\\Test.txt");
            writer.println(value);
            writer.close();


    }

    private static void WebDriverWait() {
        // TODO Auto-generated method stub

    }

}

So i´m trying to get the rate that a certain team wins game. Tried several ways of by.xpath and .getText() and ended up now with by.cssselector and still getting no value in my Test.txt. Or just isn´t it possible at this page (blocked)?

I would be really glad if someone could help me here.

Page: https://www.bet365.com/#/IP/

In regards

edit 13.03.2019

here is the html code of that page:

<div>
<div class="wc-WebConsoleModule ">
<div>
<div class="hm-HeaderModule ">
<div class="hm-HeaderModule_Primary ">
<div class="hm-BigButtons hm-BigButtons-noscrollernative "><nav class="hm-BigButtons_Inner "><a class="hm-BigButton ">Sport</a><a class="hm-BigButton hm-BigButton_Highlight ">Live</a><a class="hm-BigButton ">Casino</a><a class="hm-BigButton ">Live-Casino</a><a class="hm-BigButton ">Spiele</a><a class="hm-BigButton ">Poker</a><a class="hm-BigButton ">Vegas</a></nav></div>
<div class="hm-HeaderModule_UserAdmin ">
<div class="hm-Login ">
<div class="hm-Login_UserNameWrapper "><input class="hm-Login_InputField " type="text" />
<div class="hm-Login_InputText ">Jetzt Anmelden</div>
</div>
<div class="hm-Login_PasswordWrapper "><input class="hm-Login_InputField " type="text" /><input class="hm-Login_InputField Hidden " type="password" /><button class="hm-Login_LoginBtn " tabindex="0">OK</button>
<div class="hm-Login_InputText ">Daten vergessen?</div>
</div>
</div>
</div>
</div>
<div class="hm-HeaderModule_Secondary hm-HeaderModule_Secondary-search ">
<div class="sml-SearchHeader ">
<div class="sml-SearchHeader_SearchIcon ">&nbsp;</div>
<input class="sml-SearchTextInput " type="text" placeholder="Suchen" /></div>
<div class="hm-HeaderModule_Menus ">
<div>&nbsp;</div>
<a class="hm-HeaderModule_Link ">Spielsuchtpr&auml;vention</a><a class="hm-HeaderModule_Link ">Live-Bilder</a><a class="hm-HeaderModule_Link ">Hilfe</a>
<div>
<div class="hm-LanguageDropDownSelections hm-DropDownSelections "><a class="hm-DropDownSelections_Button hm-DropDownSelections_DropLink ">Sprache<span class="hm-DropDownSelections_Highlight ">Deutsch</span></a></div>
</div>
<div>
<div class="hm-OddsDropDownSelections hm-DropDownSelections "><a class="hm-DropDownSelections_Button hm-DropDownSelections_DropLink ">Quoten<span class="hm-DropDownSelections_Highlight ">Dezimal</span></a></div>
</div>
<div class="hm-ServicesDropDownSelections hm-DropDownSelections "><a class="hm-DropDownSelections_Button hm-DropDownSelections_DropLink hm-DropDownSelections_Solo ">Service</a></div>
</div>
<div class="hm-HeaderModule_TimeLastLogin "><time class="hm-Clock ">20:19:32 CET</time></div>
</div>
</div>
</div>
<div class="wc-PageView ">
<div class="wc-PageView_Main wc-InPlayPage_MainContainer ">
<div>
<div class="ip-InPlayModule ">
<div class="ip-ControlBar ">
<div class="ip-ControlBar_BBarItem wl-ButtonBar_Selected ">&Uuml;bersicht</div>
<div class="ip-ControlBar_BBarItem ">Einzelanzeige</div>
<div class="ip-ControlBar_BBarItem ">Kalender</div>
</div>
<div class="ipo-OverViewView ">
<div class="ipo-OverViewNativeScroller ipo-OverViewNativeScroller-scrollable ">
<div class="ipo-OverViewNativeScroller_ContentContainer " style="max-height: 424.3333282470703px;">
<div class="ipo-OverViewView_ContentContainer ipo-OverViewNativeScroller_Content ipo-ClassificationBarScrollable_RightArrowVisible ">
<div class="ipo-ClassificationBar ">
<div class="ipo-ClassificationBarScrollable ipo-ClassificationBarScrollable-notransition ">
<div class="ipo-ClassificationBarScrollable_LeftArrow ipo-ClassificationBarScrollable_Hidden ">&nbsp;</div>
<div class="ipo-ClassificationBarScrollable_HScroll">
<div class="ipo-ClassificationBar_ButtonContainer ipo-ClassificationBarScrollable_ScrollContent ipo-ClassificationBarScrollable_ScrollContentNoAnimation ">
<div class="ipo-ClassificationBarButtonBase ">
<div class="ipo-ClassificationBarButtonBase_Icon ">&nbsp;</div>
<div class="ipo-ClassificationBarButtonBase_Label ">Favoriten</div>
</div>
<div class="ipo-ClassificationBarButtonBase ipo-ClassificationBarButtonBase_Selected ipo-ClassificationBarButtonBase_Selected-1 ">
<div class="ipo-ClassificationBarButtonBase_Icon ">&nbsp;</div>
<div class="ipo-ClassificationBarButtonBase_Label ">Fu&szlig;ball</div>
</div>
<div class="ipo-ClassificationBarButtonBase ">
<div class="ipo-ClassificationBarButtonBase_Icon ">&nbsp;</div>
<div class="ipo-ClassificationBarButtonBase_Label ">Tennis</div>
</div>
<div class="ipo-ClassificationBarButtonBase ">
<div class="ipo-ClassificationBarButtonBase_Icon ">&nbsp;</div>
<div class="ipo-ClassificationBarButtonBase_Label ">Badminton</div>
</div>
<div class="ipo-ClassificationBarButtonBase ">
<div class="ipo-ClassificationBarButtonBase_Icon ">&nbsp;</div>
<div class="ipo-ClassificationBarButtonBase_Label ">Baseball</div>
</div>
<div class="ipo-ClassificationBarButtonBase ">
<div class="ipo-ClassificationBarButtonBase_Icon ">&nbsp;</div>
<div class="ipo-ClassificationBarButtonBase_Label ">Basketball</div>
</div>
<div class="ipo-ClassificationBarButtonBase ">
<div class="ipo-ClassificationBarButtonBase_Icon ">&nbsp;</div>
<div class="ipo-ClassificationBarButtonBase_Label ">E-Sports</div>
</div>
<div class="ipo-ClassificationBarButtonBase ">
<div class="ipo-ClassificationBarButtonBase_Icon ">&nbsp;</div>
<div class="ipo-ClassificationBarButtonBase_Label ">Eishockey</div>
</div>
<div class="ipo-ClassificationBarButtonBase ">
<div class="ipo-ClassificationBarButtonBase_Icon ">&nbsp;</div>
<div class="ipo-ClassificationBarButtonBase_Label ">Floorball</div>
</div>
<div class="ipo-ClassificationBarButtonBase ">
<div class="ipo-ClassificationBarButtonBase_Icon ">&nbsp;</div>
<div class="ipo-ClassificationBarButtonBase_Label ">Futsal</div>
</div>
<div class="ipo-ClassificationBarButtonBase ">
<div class="ipo-ClassificationBarButtonBase_Icon ">&nbsp;</div>
<div class="ipo-ClassificationBarButtonBase_Label ">Handball</div>
</div>
<div class="ipo-ClassificationBarButtonBase ">
<div class="ipo-ClassificationBarButtonBase_Icon ">&nbsp;</div>
<div class="ipo-ClassificationBarButtonBase_Label ">Pferderennen</div>
</div>
<div class="ipo-ClassificationBarButtonBase ">
<div class="ipo-ClassificationBarButtonBase_Icon ">&nbsp;</div>
<div class="ipo-ClassificationBarButtonBase_Label ">Snooker</div>
</div>
<div class="ipo-ClassificationBarButtonBase ">
<div class="ipo-ClassificationBarButtonBase_Icon ">&nbsp;</div>
<div class="ipo-ClassificationBarButtonBase_Label ">Squash</div>
</div>
<div class="ipo-ClassificationBarButtonBase ">
<div class="ipo-ClassificationBarButtonBase_Icon ">&nbsp;</div>
<div class="ipo-ClassificationBarButtonBase_Label ">Tischtennis</div>
</div>
<div class="ipo-ClassificationBarButtonBase ">
<div class="ipo-ClassificationBarButtonBase_Icon ">&nbsp;</div>
<div class="ipo-ClassificationBarButtonBase_Label ">Volleyball</div>
</div>
<div class="ipo-ClassificationBarButtonBase ">
<div class="ipo-ClassificationBarButtonBase_Icon ">&nbsp;</div>
<div class="ipo-ClassificationBarButtonBase_Label ">Windhundrennen</div>
</div>
</div>
</div>
<div class="ipo-ClassificationBarScrollable_RightArrow ">&nbsp;</div>
</div>
</div>
<div class="ipo-OverViewDetail ">
<div class="ipo-OverViewDetail_Container ipo-Classification ">
<div class="ipo-ClassificationHeader_Header ipo-ClassificationHeader_Header-1 ipo-ClassificationHeader_Header-lightgreenborder ipo-ClassificationHeader_Header-moremarkets ">
<div class="ipo-ClassificationHeader_HeaderLabel ">Fu&szlig;ball</div>
<div class="ipo-ClassificationHeader_EventButtonWrapper ">
<div class="ipo-ClassificationHeader_EventButtonInnerWrapper ">
<div class="ipo-ClassificationHeader_EventButton ipo-ClassificationHeader_EventButton-selected ">Alle Events</div>
<div class="ipo-ClassificationHeader_EventButton ">Live-Bilder</div>
</div>
</div>
<div class="ipo-ClassificationHeader_MarketsButtonOuterWrapper ">
<div class="ipo-ClassificationHeader_MarketsButtonWrapper ">
<div class="ipo-ClassificationHeader_MarketsButton ipo-ClassificationHeader_MarketsButton-transparent ">Alle Wettm&ouml;glichkeiten</div>
<div class="ipo-ClassificationHeader_MarketsButton ipo-InPlayClassificationMarketSelector ipo-ClassificationHeader_MarketsButton-selected ">
<div class="ipo-InPlayClassificationMarketSelector_DropDown ipo-InPlayClassificationMarketSelectorDropDown ip-DropDownContainer ">
<div class="ipo-InPlayClassificationMarketSelectorDropdownLabelContainer ">
<div class="ip-DropDownContainer_Button ipo-InPlayClassificationMarketSelectorDropDown_Button ipo-InPlayClassificationMarketSelectorDropDown_Button-1 ">Haupt-Wettm&ouml;glichkeiten</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="ipo-FavouritesContainer ">&nbsp;</div>
<div class="ipo-CompetitionRenderer ">
<div class="ipo-Competition ipo-Competition-open ">
<div class="ipo-CompetitionButton ">
<div class="ipo-CompetitionButton_NameLabel ipo-CompetitionButton_NameLabelHasMarketHeading ">Deutschland - 3. Liga</div>
<div class="ipo-CompetitionButton_MarketHeadingWrapper">
<div class="ipo-CompetitionButton_MarketHeadingLabel ">1X2</div>
<div class="ipo-CompetitionButton_MarketHeadingLabel ">N&auml;chstes Tor</div>
<div class="ipo-CompetitionButton_MarketHeadingLabel ">Tore - Spiel</div>
</div>
</div>
<div class="ipo-Competition_FavouriteWrapper">&nbsp;</div>
<div class="ipo-FixtureRenderer ipo-Competition_Container ">
<div class="ipo-Fixture ipo-Fixture_CL1 ipo-Fixture-hastimer ipo-Fixture_MainMarkets ">
<div class="ipo-Fixture_TableRow ">
<div class="ipo-Fixture_ScoreDisplay ipo-ScoreDisplayStandard ">
<div class="ipo-ScoreDisplayStandard_Wrapper ">
<div class="ipo-InPlayTimer ">58:27</div>
<div class="ipo-TeamStack ">
<div class="ipo-TeamStack_Team"><span class="ipo-TeamStack_TeamWrapper ">1. FC Kaiserslautern</span>
<div class="ipo-SoccerTeamStack_TeamGoalMarker "><span class="ipo-SoccerTeamStack_TeamGoalMarkerSpan ">TOR</span></div>
</div>
<div class="ipo-TeamStack_Team"><span class="ipo-TeamStack_TeamWrapper ">Eintracht Braunschweig</span>
<div class="ipo-SoccerTeamStack_TeamGoalMarker "><span class="ipo-SoccerTeamStack_TeamGoalMarkerSpan ">TOR</span></div>
</div>
<div class="ipo-TeamStack_Team"><span class="ipo-TeamStack_TeamWrapper ipo-TeamStack_DrawWrapper">Unentschieden</span></div>
</div>
<div class="ipo-TeamPoints ">
<div class="ipo-TeamPoints_TeamScoresWrapper ">
<div class="ipo-TeamPoints_TeamScore ipo-TeamPoints_TeamScore-teamone ">0</div>
<div class="ipo-TeamPoints_TeamScore ipo-TeamPoints_TeamScore-teamtwo ">0</div>
</div>
</div>
<div class="ipo-ScoreDisplayStandard_MediaButton ">&nbsp;</div>
</div>
</div>
<div class="ipo-MainMarkets ">
<div class="ipo-MainMarketRenderer ">
<div class="gl-ParticipantCentered gl-ParticipantCentered_NoHandicap ipo-AllMarketsParticipant gl-ParticipantCentered_BlankName "><span class="gl-ParticipantCentered_Odds">1.83</span></div>
<div class="gl-ParticipantCentered gl-ParticipantCentered_NoHandicap ipo-AllMarketsParticipant gl-ParticipantCentered_BlankName "><span class="gl-ParticipantCentered_Odds">11.00</span></div>
<div class="gl-ParticipantCentered gl-ParticipantCentered_NoHandicap ipo-AllMarketsParticipant gl-ParticipantCentered_BlankName "><span class="gl-ParticipantCentered_Odds">2.30</span></div>
</div>
<div class="ipo-MainMarketRenderer ">
<div class="gl-ParticipantCentered gl-ParticipantCentered_NoHandicap ipo-AllMarketsParticipant ipo-ParticipantNoGoal gl-ParticipantCentered_BlankName "><span class="gl-ParticipantCentered_Odds">1.72</span></div>
<div class="gl-ParticipantCentered gl-ParticipantCentered_NoHandicap ipo-AllMarketsParticipant ipo-ParticipantNoGoal gl-ParticipantCentered_BlankName "><span class="gl-ParticipantCentered_Odds">6.50</span></div>
<div class="gl-ParticipantCentered gl-ParticipantCentered_NoHandicap ipo-AllMarketsParticipant ipo-ParticipantNoGoal gl-ParticipantCentered_BlankName "><span class="gl-ParticipantCentered_Name">Kein 1.</span><span class="gl-ParticipantCentered_Odds">2.62</span></div>
</div>
<div class="ipo-MainMarketRenderer ">
<div class="gl-ParticipantCentered ipo-AllMarketsParticipant gl-ParticipantCentered_BlankName "><span class="gl-ParticipantCentered_Handicap">&Uuml; 0.5</span><span class="gl-ParticipantCentered_Odds">1.44</span></div>
<div class="gl-ParticipantCentered ipo-AllMarketsParticipant gl-ParticipantCentered_BlankName "><span class="gl-ParticipantCentered_Handicap">U 0.5</span><span class="gl-ParticipantCentered_Odds">2.62</span></div>
<div class="ipo-MainMarketRenderer_BlankParticipant ">&nbsp;</div>`

I want to get the "1.83" in here <div class="gl-ParticipantCentered gl-ParticipantCentered_NoHandicap ipo-AllMarketsParticipant gl-ParticipantCentered_BlankName "><span class="gl-ParticipantCentered_Odds">1.83</span></div>

edit 13.03.2019

Now i am receiving this message:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: div.ipo-Competition:nth-child(1) > div:nth-child(3) > div:nth-child(2) > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > span:nth-child(2)

edit 14.03.2019

Later, if its working to catch the rate, i would like to make a loop to get the rates of all games with x = 1->n "div:nth-child(x)".

2 answers

  • answered 2019-03-13 18:27 Josh

    Just checking - Are you trying to get the value out of a button element ? I think you may need to try to capture it in a webElement object (org.openqa.selenium.WebElement). This is a sample from one of my old Cucumber Test cases which pulls the value from a button element.

    WebElement okButton = driver.findElement(By.id("confirmOk"));
    WebElement cancelButton = driver.findElement(By.id("confirmCancle"));
    assertEquals(okButton.getText(),"OK");
    assertEquals(cancelButton.getText(),"Cancel");
    

    You can use either By.Id or By.Xpath or By.cssselector but I think you just need that one extra step to capture the value. Not sure if that answers your question.

  • answered 2019-03-13 20:10 JeffC

    So the problem is that you are declaring a string value as null and then in the next line doing .getAttribute(value) which is .getAttribute(null) in this case.

    String value = null;
    driver.findElement(By.cssSelector(...)).getAttribute(value);
    

    My guess is that you want to do something more like the below to get the text. You need to delete the first line, store the return in the value variable so that it will get printed.

    // String value = null; <- DELETE THIS LINE
    String value = driver.findElement(By.cssSelector(...)).getText();