【问题标题】:Python - selenium unable to get element text with PhantomJSPython - selenium 无法使用 PhantomJS 获取元素文本
【发布时间】:2017-07-16 06:21:09
【问题描述】:

我可以获取其他tds 中的所有文本,除了tdclass="datapid"

  <tr>
     <td class="cregion">JP</td>
     <td class="datacompany"><a href="/company/10644-tecmo">Tecmo</a></td>
     <td class="datapid">P99-00001</td>
     <td class="datapid">4960677070033</td>
     <td class="cdate">02/06/03</td>
     <td class="datarating">&nbsp;</td>
  </tr>

我在 PhantomJS 2.1.1 中使用 selenium:

driver = webdriver.PhantomJS(
"c:/phantomjs-2.1.1-windows/bin/phantomjs.exe")

试图获取字段,但只有datapid 字段为空白:

    cregions = driver.find_elements(
    By.CLASS_NAME, "cregion")
    for region in cregions:
        regions.append(region.text)

    datacompanies = driver.find_elements(
    By.CLASS_NAME, "datacompany")
    for company in datacompanies:
        publishers.append(company.find_element(By.XPATH, "a").text)

    datapids = driver.find_elements(
    By.CLASS_NAME, "datapid")
    for i in range(0, (len(datapids))):
        if i % 2:
            pids.append(datapids[i].text)  //datapids[i].text = ''

【问题讨论】:

  • datapids 中的 pid 会发生什么:pids.append(pid.text)

标签: python selenium phantomjs


【解决方案1】:

最大化窗口后:

driver.maximize_window()

现在我可以获取div 中的文本了。

我认为这是因为webdriver 不是简单地读取源代码,而是从窗口中获取它。如果node在窗口外,则webdriver读不到

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 2018-01-21
    • 2020-05-30
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多