简体   繁体   中英

Selenium with PhantomJS returning empty text string for WebElement

I am attempting to read the number of minutes that has passed in a football game from the betfair website. I understand that the content and text that I am attempting to read is generated by javascript, so am attempting to get it as follows:

def _get_time_elapsed_soccer(self, market_id):
    url = self._get_market_betfair_url(market_id)
    self._driver.get(url)

    try:
        time_elapsed_elem = WebDriverWait(self._driver, 10).until(
            EC.presence_of_element_located((By.CLASS_NAME, 'time-elapsed')))
        time_elapsed_txt = time_elapsed_elem.text

        print(time_elapsed_elem)
        print(time_elapsed_txt)
        print('length of string:', len(time_elapsed_txt))

    except TimeoutException:
        print('exception')
    finally:
        pass

and the url is generated by the method

def _get_market_betfair_url(self, market_id):
    try:
        event_info = self._get_event_type_from_market_id(market_id)
        event_type_name = str.lower(event_info['name'])

        # replace any spaces with hyphens
        event_type_name = event_type_name.replace(' ', '-')

        # replace soccer with football
        event_type_name = event_type_name.replace('soccer', 'football')

        # construct the url
        url = ''.join(['https://www.betfair.com/exchange/plus/', event_type_name, '/market/', str(market_id)])
        return url
    except TypeError:
        return False

the code in _get_time_elapsed_seccer prints out:

<selenium.webdriver.remote.webelement.WebElement (session="d58e0c50-9d45-11e7-a933-37f14018586f", element=":wdc:1505830903385")>
text:
length of string: 0

The web element is clearly returned, but the text that I can see on the page is not included. Once the javascript is loaded on the page the html looks like:

...
<p class="time-elapsed inplay" ng-class="{ inplay: sportsHeaderCtrl.data.event.status !== 'Finished' }">
    <!----> <!----><span ng-if="!sportsHeaderCtrl.data.moduleConfig.eventStatus[sportsHeaderCtrl.data.event.status]">25'</span>
    <!----><halftime-fulltime eventtypeid="sportsHeaderCtrl.data.event.eventTypeId" score="sportsHeaderCtrl.data.event.score" status="sportsHeaderCtrl.data.event.status"><!----></halftime-fulltime>
</p>
...

Should my python code not be able to get the text "25'" from the

tag with class-name 'time-elapsed'?

You can view an example of the a similar url that I have been using by looking on any in-game football match at https://www.betfair.com/exchange/plus/football/

The url is simply the above url with the specific market-id at the end.

In an ideal world if I could simply run the javascript function through selenium that returns the time elapsed that would be great but I am not sure if this is possible!

I found the answer to my question in this link Python Selenium: Finds h1 element but returns empty text string

I think the explanation there explains the issue well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM