简体   繁体   English

无法使用Selenium和Chrome获取元素文本

[英]Unable to get element text using Selenium with Chrome

I'm trying to scrape Merriam-Webster's Medical Dictionary for medical terms using Python and Chrome as the Selenium webdriver. 我正在尝试使用Python和Chrome作为Selenium webdriver来搜索Merriam-Webster的医学词典。 So far, this is what I have: 到目前为止,这就是我所拥有的:

    from os import path
    from selenium import webdriver

    # Adding an ad-blocker to Chrome to speed up page load times
    options = webdriver.ChromeOptions()
    options.add_extension(path.abspath("ublock-origin.crx"))

    # Declaring the Selenium webdriver
    driver = webdriver.Chrome(chrome_options = options)

    # Fetching the "A" terms as a test set
    driver.get("https://www.merriam-webster.com/browse/medical/a")

    scraped_words = []  # The list that will hold each word
    page_num = 1
    while page_num < 55:  # There are 54 pages of "A" terms
        try:
            for i in range(4):  # There are 3 columns per page of words
                column = "/html/body/div/div/div[5]/div[2]/div[1]/div/div[3]/ul/li[" + str(i) + "]/a"
                number_of_words = len(driver.find_elements_by_xpath(column))
                for j in range(number_of_words):
                    word = driver.find_elements_by_xpath(column + "[" + str(j) + "]")
                    scraped_words.append(word)
            driver.find_element_by_class_name("fa-angle-right").click()  # Next page
            page_num += 1  # Increment page number to keep track of current page
        except:
            driver.close()

    # Write out words to a file
    with open("medical_terms.dict", "w") as text_file:
        for i in range(len(scraped_words)):
            text_file.write(str(scraped_words[i]))
            text_file.write("\n")

    driver.close()

The above code fetches all the items, as the output of len(scraped_words) is the number expected. 上面的代码获取所有项目,因为len(scraped_words)的输出是预期的数字。 However, since I did not specify that I wanted to fetch the text of the elements, I get element identifiers (I think?) instead of text. 但是,由于我没有指定我想获取元素的文本,因此我得到元素标识符(我认为?)而不是文本。 If I decide to use word = driver.find_elements_by_xpath(column + "[" + str(j) + "]").text in order to specify that I want to get the text of the element, I get the following error: 如果我决定使用word = driver.find_elements_by_xpath(column + "[" + str(j) + "]").text以指定我想获取元素的文本,我会收到以下错误:

Traceback (most recent call last):
  File "mw_download.py", line 20, in <module>
    number_of_words = len(driver.find_elements_by_xpath(column))
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 325, in find_elements_by_xpath
    return self.find_elements(by=By.XPATH, value=xpath)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 817, in find_elements
    'value': value})['value']
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: no such session
  (Driver info: chromedriver=2.31.488774 (7e15618d1bf16df8bf0ecf2914ed1964a387ba0b),platform=Mac OS X 10.12.6 x86_64)


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "mw_download.py", line 27, in <module>
    driver.close()
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 541, in close
    self.execute(Command.CLOSE)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: no such session
  (Driver info: chromedriver=2.31.488774 (7e15618d1bf16df8bf0ecf2914ed1964a387ba0b),platform=Mac OS X 10.12.6 x86_64)

What is strange to me here is that the only code I change between runs is on line 22 yet the error message points out line 20 instead. 我在这里感到奇怪的是,我在运行之间更改的唯一代码是在第22行,但错误消息指出了第20行。

Any help in deciphering what's going on here and what I can do to fix it would be much appreciated! 任何帮助破译这里发生的事情以及我可以做些什么来修复它将非常感谢! :+) :+)

You just need to create a words list accessing your elements texts, changing: 您只需创建一个访问元素文本的words列表,即可更改:

word = driver.find_elements_by_xpath(column + "[" + str(j) + "]")

to: 至:

word = [i.text for i in driver.find_elements_by_xpath(column + "[" + str(j) + "]")]

Because .find_elements_by_xpath will always return a list, accessing .text directly won't work. 因为.find_elements_by_xpath将始终返回一个列表,直接访问.text将不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 selenium webdriver 获取元素文本 - Unable to get element text using selenium webdriver Python-Selenium无法使用PhantomJS获取元素文本 - Python - selenium unable to get element text with PhantomJS 无法在 python selenium 中使用 selenium chrome webdriver 定位元素 - Unable to locate element using selenium chrome webdriver in python selenium 无法使用Selenium获取网站中的表格元素 - Unable to get table element in website using Selenium "Selenium 仅在使用无头铬(Python)时无法定位元素" - Selenium Unable to locate element only when using headless chrome (Python) 无法使用 Selenium 从网页获取文本 - Unable to get the text from a webpage using Selenium 使用 selenium 从 html 元素中获取文本 - Get text from html element using selenium Python Selenium Chrome 无法找到没有此类元素的元素 - Python Selenium Chrome Unable to locate element no such element selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法使用带有Python的Selenium ChromeDriver Chrome找到元素 - selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element using Selenium ChromeDriver Chrome with Python selenium 使用 chrome webdriver 访问具有更改 ID 的文本输入元素 - selenium accessing text input element with changing ID using chrome webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM