Попытка загрузить файл через хром: Selenium Python - PullRequest
0 голосов
/ 21 октября 2018

Я использую Selenium с Python для загрузки определенных файлов с веб-сайта.Я пытаюсь получить доступ к опции «Просмотреть все продукты >>».Оттуда вы можете выбрать тип файла (CSV, XLSX), а затем вы должны легко загрузить его.Моя проблема в том, что я не могу получить доступ к этой области «Просмотр всех продуктов».Я пробовал во многих отношениях.Я приложу свой код и скриншот со структурой сайта.Я не могу опубликовать весь HTML, потому что это веб-сайт, доступный только для платных пользователей.

enter image description here.Пять ссылок:

  • 1 и 2. (Здесь «Просмотреть все продукты» появляется дважды):

    <a class="yui3-c-reportdashboardwidget-reportLink" href="./embedded.html?showGDLogo=false#project=/gdc/projects/e05jwlnny6rlxyt5ib9r6479279crrq8&amp;dashboard=/gdc/md/e05jwlnny6rlxyt5ib9r6479279crrq8/obj/4817&amp;tab=8473392139f7&amp;s=/gdc/projects/e05jwlnny6rlxyt5ib9r6479279crrq8|analysisPage|head|/gdc/md/e05jwlnny6rlxyt5ib9r6479279crrq8/obj/3630" title="Headline - View All Products Link" target="_self">Headline - View All Products Link</a>
    
  • 3 и 4.(Здесь «Просмотреть все товары» появляется дважды):

    <span class="yui3-c-reportdashboardwidget-reportLabel" title="Headline - View All Products Link">Headline - View All Products Link</span>
    
  • 5:

    <div class="number" style="font-size: 16px; color: rgb(0, 61, 76);" id="yui_3_14_1_1_1540109592048_72886">      View all products &gt;&gt;</div>
    

Ссылка, которую я хочу обработатьэто число "5", потому что я думаю, что это то, что мне нужно щелкнуть (), чтобы потом я мог загрузить отчет.

Мой код для этой части:

Просмотреть все продуктыКнопка

#product_button = driver.find_elements_by_xpath("//div[@class='c-oneNumberReport yui3-widget yui3-c-onenumberreport yui3-c-onenumberreport-content yui3-widget-content-expanded drillable']")[-1]
#product_button = driver.find_element_by_xpath(("//div[text()='View all products >>']"))
product_button = driver.find_elements_by_xpath("//a[@class='ember-view reportInfoPanelHandle point-to-top']")[-3]
product_button.click()
#product_button.send_keys(Keys.ENTER)
#####actions = ActionChains(driver)
#actions.move_to_element(product_button).send_keys(Keys.ENTER)
###########actions.move_to_element(product_button)
###########actions.click()
#actions.sendKeys(Keys.Return);
#actions.build().perform()

Обновление ::::

enter image description here enter image description here

Вот HTML-код для«Загрузить как ..»:

<span class="button-text"><script id="metamorph-39-start" type="text/x-placeholder"></script>Download as...<script id="metamorph-39-end" type="text/x-placeholder"></script></span>

HTML для «CSV (необработанные данные)»:

<ul id="ember2849" class="ember-view reportExportMenu gdc-menu-simple" style="position: absolute; top: 106px; left: 15px; z-index: 3005;"><li id="ember2850" class="ember-view reportExportMenuItem">
<a data-ember-action="17"><script id="metamorph-47-start" type="text/x-placeholder"></script>PDF (Portrait)<script id="metamorph-47-end" type="text/x-placeholder"></script></a>
</li><li id="ember2851" class="ember-view reportExportMenuItem">
<a data-ember-action="18"><script id="metamorph-48-start" type="text/x-placeholder"></script>PDF (Landscape)<script id="metamorph-48-end" type="text/x-placeholder"></script></a>
</li><li id="ember2852" class="ember-view reportExportMenuItem">
<a data-ember-action="19"><script id="metamorph-49-start" type="text/x-placeholder"></script>XLSX...<script id="metamorph-49-end" type="text/x-placeholder"></script></a>
</li><li id="ember2853" class="ember-view reportExportMenuItem">
<a data-ember-action="20"><script id="metamorph-50-start" type="text/x-placeholder"></script>CSV (formatted)<script id="metamorph-50-end" type="text/x-placeholder"></script></a>
</li><li id="ember2854" class="ember-view reportExportMenuItem">
<a data-ember-action="21"><script id="metamorph-51-start" type="text/x-placeholder"></script>CSV (raw data)<script id="metamorph-51-end" type="text/x-placeholder"></script></a>
</li></ul>

1 Ответ

0 голосов
/ 21 октября 2018

Попробуйте использовать код ниже, чтобы щелкнуть необходимый элемент:

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

link = wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//div[starts-with(normalize-space(), "View all products")]')))
driver.execute_script("arguments[0].scrollIntoView();", link)
link.click()
...