How to extract the text from the "Health" Element

Viewed 85

I've been trying for hours to find the correct link to use in selenium for the HP value of this game.

To check yourself, I'll provide a user & password as belowed:

Website : https://s3-en.bitefight.gameforge.com/user/login

Username: testaccount111

Password: python123

enter image description here

I've tried health = driver.find_elements_by_xpath("//div[2]/div/div[1]/text()[5]") but it gives errors.

3 Answers

use below xpath to locate you element ( marked in image )

//img[@alt='Health']/preceding-sibling::text()[1]

but selenium doesn't allow you to locate an element using text node. alternative way is you can use javascript executor to evaluate the xpath.

I have below code reference in Java. please change as per python

JavascriptExecutor js = (JavascriptExecutor)driver;
Object health= js.executeScript("var value = document.evaluate(\"//img[@alt='Health']/preceding-sibling::text()[1]\",document, null, XPathResult.STRING_TYPE, null ); return value.stringValue;"); 
System.out.println(health.toString().trim());

OR

WebElement element = driver.findElement(By.xpath("//div[@class='gold']"));
JavascriptExecutor js = (JavascriptExecutor)driver;
String health= (String) js.executeScript("return arguments[0].childNodes[8].textContent", element);
System.out.println(health.trim());

@NarendraR has good answer, in python the code to get result as 21500 is

health = driver.execute_script('''
  var value = document.evaluate("//img[@alt='Health']/preceding-sibling::text()[1]",document, null, XPathResult.STRING_TYPE, null );
  return value.stringValue.split('/')[0].trim().replace('.', '');
''');

and for another method

elem = driver.find_element_by_xpath('//div[@class="gold"]')
health = re.search(r'(\d+\.\d+)\s+/', elem.text).group(1)
health = int(health.replace('.', ''))

As per the website https://s3-en.bitefight.gameforge.com/user/login to extract the text from Health element i.e. 8.460 / 21.500 you need to induce WebDriverWait for the desired element to be visible and you can use the following solution:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_argument('disable-infobars')
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://s3-en.bitefight.gameforge.com/user/login")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='input' and @name='user']"))).send_keys("testaccount111")
    driver.find_element_by_xpath("//input[@class='input' and @name='pass']").send_keys("python123")
    driver.find_element_by_xpath("//input[@class='btn-small' and @value='Login']").click()
    WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='gold']//img[contains(@src,'/img/symbols')]")))
    health_text = driver.find_element_by_xpath("//div[@class='gold']").get_attribute("textContent").splitlines()[5]
    print(health_text)
    
  • Console Output:

          8.460 / 21.500
    
Related