更新时间:2023-02-19 16:58:57
要打印 text ... 你必须诱导 WebDriverWait 用于 visibility_of_element_located()
并且您可以使用以下任一 定位器策略:
To print text ... you have to induce WebDriverWait for the visibility_of_element_located()
and you can use either of the following Locator Strategies:
使用 CSS_SELECTOR
、childNodes 和 strip()
:
print(driver.execute_script('return arguments[0].firstChild.textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.call_recipe[href^='/recipes']")))).strip())
使用 XPATH
、get_attribute()
和 splitlines()
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@class='call_recipe' and starts-with(@href, '/recipes')]"))).get_attribute("innerHTML").splitlines()[1])
注意:您必须添加以下导入:
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
您可以在以下位置找到一些相关的详细讨论:
You can find a couple of relevant detailed discussions in: