Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 从aria标签selenium webdriver(python)中提取文本_Python 3.x_Selenium_Selenium Webdriver_Webdriver_Webdriverwait - Fatal编程技术网

Python 3.x 从aria标签selenium webdriver(python)中提取文本

Python 3.x 从aria标签selenium webdriver(python)中提取文本,python-3.x,selenium,selenium-webdriver,webdriver,webdriverwait,Python 3.x,Selenium,Selenium Webdriver,Webdriver,Webdriverwait,现在,我正在开发一个程序,该程序接受用户输入的问题和答案,将它们分成单独的q和a列表,然后自动回答给定问题或答案的问题。由于使用“bot”的地方是在线的,所以我使用的是SeleniumWeb驱动程序,这在尝试读取aria标签时给我带来了一些问题。我不知道我做错了什么,因为我对selenium、HTML或CSS一点也不了解。我试图在不知道每个容器是什么的情况下找到每个容器的aria标签值 我试图获取文本值的HTML示例: <div class="MatchModeQuestionG

现在,我正在开发一个程序,该程序接受用户输入的问题和答案,将它们分成单独的q和a列表,然后自动回答给定问题或答案的问题。由于使用“bot”的地方是在线的,所以我使用的是SeleniumWeb驱动程序,这在尝试读取aria标签时给我带来了一些问题。我不知道我做错了什么,因为我对selenium、HTML或CSS一点也不了解。我试图在不知道每个容器是什么的情况下找到每个容器的aria标签值

我试图获取文本值的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)
输出:
将文本(例如隐藏起来);隐瞒真相;提供站不住脚的借口存在于子
及其父
中。因此,您需要将其提取出来,以便对所有元素的可见性进行归纳(),并且您可以使用以下任一选项:

  • 使用
    CSS\u选择器
    get\u属性()

  • 使用
    XPATH
    get\u attribute()

  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
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]")))])
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