从 aria-label selenium webdriver (python) 中提取文本
Extract text from an aria-label selenium webdriver (python)
现在我正在开发一个程序,它接受用户输入的问题和答案,将它们分成单独的 q 和 a 列表,然后根据问题或答案自动回答问题。由于使用 'bot' 的地方在线,我使用的是 Selenium 网络驱动程序,这在尝试读取 aria-label 时导致我出现一些问题。我不知道自己做错了什么,因为我对 selenium、HTML 或 CSS 一点都不了解。我试图在不知道它是什么的情况下为每个容器找到 aria-label 值
HTML 的示例我正在尝试获取文本值:
<div class="MatchModeQuestionGridBoard-tile"><div class="MatchModeQuestionGridTile" touch-action="auto"><div class="MatchModeQuestionGridTile-content"><div aria-label="to cloak; to conceal the truth; to offer lame excuses" class="FormattedText notranslate TermText MatchModeQuestionGridTile-text lang-en" style="font-size: 14px;"><div style="display: block;">to cloak; to conceal the truth; to offer lame excuses</div></div></div></div></div>
我的代码片段:
def driver():
driver = webdriver.Chrome()
driver.get(link)
startMatch = driver.find_element_by_xpath("/html/body/div[5]/div/div/div/div[2]/button").click()
#find text in matches
container = driver.find_elements_by_class_name('MatchModeQuestionGridTile-content')
containerFile = open("QuizletTerms.txt", "w+")
for _ in list(container):
arialabel = driver.find_elements_by_css_selector("div[aria-label='']")
containerFile.write("\n")
containerFile.write(str(arialabel))
print(arialabel)
containerFile.close()
print("done")
sleep(5)
输出:
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
文本例如披风;隐瞒真相;提供蹩脚的借口 出现在 child <div>
中,也出现在 parent <div>
中。所以提取它你需要诱导 for visibility_of_all_elements_located()
and you can use either of the following :
使用 CSS_SELECTOR
和 get_attribute()
:
print([my_elem.get_attribute("aria-label") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.MatchModeQuestionGridTile-content>div[aria-label]")))])
使用 XPATH
和 get_attribute()
:
print([my_elem.get_attribute("aria-label") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='MatchModeQuestionGridTile-content']/div[@aria-label]")))])
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
现在我正在开发一个程序,它接受用户输入的问题和答案,将它们分成单独的 q 和 a 列表,然后根据问题或答案自动回答问题。由于使用 'bot' 的地方在线,我使用的是 Selenium 网络驱动程序,这在尝试读取 aria-label 时导致我出现一些问题。我不知道自己做错了什么,因为我对 selenium、HTML 或 CSS 一点都不了解。我试图在不知道它是什么的情况下为每个容器找到 aria-label 值
HTML 的示例我正在尝试获取文本值:
<div class="MatchModeQuestionGridBoard-tile"><div class="MatchModeQuestionGridTile" touch-action="auto"><div class="MatchModeQuestionGridTile-content"><div aria-label="to cloak; to conceal the truth; to offer lame excuses" class="FormattedText notranslate TermText MatchModeQuestionGridTile-text lang-en" style="font-size: 14px;"><div style="display: block;">to cloak; to conceal the truth; to offer lame excuses</div></div></div></div></div>
我的代码片段:
def driver():
driver = webdriver.Chrome()
driver.get(link)
startMatch = driver.find_element_by_xpath("/html/body/div[5]/div/div/div/div[2]/button").click()
#find text in matches
container = driver.find_elements_by_class_name('MatchModeQuestionGridTile-content')
containerFile = open("QuizletTerms.txt", "w+")
for _ in list(container):
arialabel = driver.find_elements_by_css_selector("div[aria-label='']")
containerFile.write("\n")
containerFile.write(str(arialabel))
print(arialabel)
containerFile.close()
print("done")
sleep(5)
输出:
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
文本例如披风;隐瞒真相;提供蹩脚的借口 出现在 child <div>
中,也出现在 parent <div>
中。所以提取它你需要诱导 visibility_of_all_elements_located()
and you can use either of the following
使用
CSS_SELECTOR
和get_attribute()
:print([my_elem.get_attribute("aria-label") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.MatchModeQuestionGridTile-content>div[aria-label]")))])
使用
XPATH
和get_attribute()
:print([my_elem.get_attribute("aria-label") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='MatchModeQuestionGridTile-content']/div[@aria-label]")))])
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC