【问题标题】:Print all values in a column from table Selenium从表 Selenium 中打印列中的所有值
【发布时间】:2020-09-24 23:15:25
【问题描述】:

我想通过此链接返回 MeSH 列中的所有值:https://ii.nlm.nih.gov/cgi-bin/II/Interactive/checkPubMed.pl。 该链接不存储搜索结果,因此要访问实际链接,请转到此处https://ii.nlm.nih.gov/Interactive/MTI/phrase2mesh.shtml 并搜索“杆状病毒”并按提交。

driver.get('https://ii.nlm.nih.gov/Interactive/MTI/phrase2mesh.shtml')
#excluded code here but basically input "baculovirus" into the search bar and search
#brings you to this link https://ii.nlm.nih.gov/cgi-bin/II/Interactive/checkPubMed.pl
table = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.TAG_NAME,"table")))
        for row in table(3): #to get the third column of the table
            print(row.text)

不知道如何前进,任何帮助将不胜感激!

【问题讨论】:

  • 您好,您希望输出是什么?

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

您可以简单地执行以下操作。

table = WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.XPATH,"//table[2]/tbody/tr/td[3]")))
for td in table: #to get the third column of the table
    print(td.text)

【讨论】:

  • for td in table: 有问题,webelement 不可迭代
  • @Brian Guan 哦,我的错我忘了改变存在
【解决方案2】:

要打印 MeSH Term 列中的所有值,您需要为 visibility_of_all_elements_located() 引入 WebDriverWait,您可以使用以下任一 Locator Strategies

  • 使用CSS_SELECTORtext属性:

    driver.get('https://ii.nlm.nih.gov/Interactive/MTI/phrase2mesh.shtml')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "textarea[name='InputText']"))).send_keys("baculovirus")
    driver.find_element_by_css_selector("input[value='Submit Phrase2MeSH Request']").click()
    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "body table:nth-of-type(2) tr td:nth-child(3)")))])
    
  • 使用XPATHget_attribute()

    driver.get('https://ii.nlm.nih.gov/Interactive/MTI/phrase2mesh.shtml')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//textarea[@name='InputText']"))).send_keys("baculovirus")
    driver.find_element_by_xpath("//input[@value='Submit Phrase2MeSH Request']").click()
    print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//b[starts-with(., 'Descriptors')]//following::table[1]//tr//td[not(contains(@align,'right'))]")))])
    
  • 控制台输出:

    ['Baculoviridae', 'Recombinant Proteins', 'Genetic Vectors', 'Sf9 Cells', 'Nucleopolyhedroviruses', 'Cell Line', 'Spodoptera']
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

参考文献

您可以在NoSuchElementException 上找到一些相关讨论:

【讨论】:

    猜你喜欢
    • 2020-11-07
    • 2020-03-30
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    相关资源
    最近更新 更多