如何从 "Health" 元素中提取文本
How to extract the text from the "Health" Element
我已经尝试了数小时以找到正确的 link 以在 selenium 中用于此游戏的 HP 值。
为了检查您自己,我将提供如下用户名和密码:
Website : https://s3-en.bitefight.gameforge.com/user/login
Username: testaccount111
Password: python123
我试过了health = driver.find_elements_by_xpath("//div[2]/div/div[1]/text()[5]")
但它给出了错误。
使用下面的 xpath 定位您的元素(在图像中标记)
//img[@alt='Health']/preceding-sibling::text()[1]
但是 selenium 不允许您使用文本节点定位元素。另一种方法是您可以使用 javascript 执行程序来评估 xpath。
我在 Java 中有以下代码参考。请根据 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());
或
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 有很好的答案,在 python 中获得结果的代码 21500
是
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('.', '');
''');
以及另一种方法
elem = driver.find_element_by_xpath('//div[@class="gold"]')
health = re.search(r'(\d+\.\d+)\s+/', elem.text).group(1)
health = int(health.replace('.', ''))
根据网站 https://s3-en.bitefight.gameforge.com/user/login
从 Health 元素中提取文本,即 8.460 / 21.500 您需要诱导 WebDriverWait 使所需的元素可见,您可以使用以下解决方案:
代码块:
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)
控制台输出:
8.460 / 21.500
我已经尝试了数小时以找到正确的 link 以在 selenium 中用于此游戏的 HP 值。
为了检查您自己,我将提供如下用户名和密码:
Website : https://s3-en.bitefight.gameforge.com/user/login
Username: testaccount111
Password: python123
我试过了health = driver.find_elements_by_xpath("//div[2]/div/div[1]/text()[5]")
但它给出了错误。
使用下面的 xpath 定位您的元素(在图像中标记)
//img[@alt='Health']/preceding-sibling::text()[1]
但是 selenium 不允许您使用文本节点定位元素。另一种方法是您可以使用 javascript 执行程序来评估 xpath。
我在 Java 中有以下代码参考。请根据 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());
或
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 有很好的答案,在 python 中获得结果的代码 21500
是
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('.', '');
''');
以及另一种方法
elem = driver.find_element_by_xpath('//div[@class="gold"]')
health = re.search(r'(\d+\.\d+)\s+/', elem.text).group(1)
health = int(health.replace('.', ''))
根据网站 https://s3-en.bitefight.gameforge.com/user/login
从 Health 元素中提取文本,即 8.460 / 21.500 您需要诱导 WebDriverWait 使所需的元素可见,您可以使用以下解决方案:
代码块:
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)
控制台输出:
8.460 / 21.500