在动态 XPath 中获取值
Getting a Value in a Dyanmic XPath
number_of_selected_players = self.driver.find_element_by_xpath("/html[1]/body[1]/main[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[2]/div[4]/div[1]/div[1]/div[1]").value()
print(number_of_selected_players)
我想从这个特定元素中获取值:
但我最终得到了这个错误:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html[1]/body[1]/main[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[2]/div[4]/div[1]/div[1]/div[1]"}
(Session info: chrome=85.0.4183.83)
我该如何解决这个问题,或者您能否指出与此类似的问题,以便我了解我犯了什么错误?
谢谢。
您可以使用 following-sibling xpath 命令获取“0 / 15”。
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(self.driver, 50).until(
EC.presence_of_all_elements_located
((By.XPATH,
'//h3[text()="Players Selected"]/following-sibling::div'))
)
number_of_selected_players = self.driver.find_element_by_xpath('//h3[text()="Players Selected"]/following-sibling::div').text
print(number_of_selected_players)
number_of_selected_players = self.driver.find_element_by_xpath("/html[1]/body[1]/main[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[2]/div[4]/div[1]/div[1]/div[1]").value()
print(number_of_selected_players)
我想从这个特定元素中获取值:
但我最终得到了这个错误:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html[1]/body[1]/main[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[2]/div[4]/div[1]/div[1]/div[1]"}
(Session info: chrome=85.0.4183.83)
我该如何解决这个问题,或者您能否指出与此类似的问题,以便我了解我犯了什么错误?
谢谢。
您可以使用 following-sibling xpath 命令获取“0 / 15”。
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(self.driver, 50).until(
EC.presence_of_all_elements_located
((By.XPATH,
'//h3[text()="Players Selected"]/following-sibling::div'))
)
number_of_selected_players = self.driver.find_element_by_xpath('//h3[text()="Players Selected"]/following-sibling::div').text
print(number_of_selected_players)