如何从“健康"菜单中提取文本元素 [英] How to extract the text from the "Health" Element
问题描述
我一直在努力寻找适合硒的正确链接,以达到该游戏的HP值.
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:
用户名:testaccount111
Username: testaccount111
密码:python123
Password: python123
我尝试了health = driver.find_elements_by_xpath("//div[2]/div/div[1]/text()[5]")
但这会带来错误.
I've tried health = driver.find_elements_by_xpath("//div[2]/div/div[1]/text()[5]")
but it gives errors.
推荐答案
根据网站https://s3-en.bitefight.gameforge.com/user/login
从 Health 元素(即 8.460/21.500 )中提取文本需要诱使 WebDriverWait 使所需的元素可见,您可以使用以下解决方案:
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
这篇关于如何从“健康"菜单中提取文本元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!