Python Selenium CSS Selector: Element is not visible

这一生的挚爱 提交于 2019-12-24 09:58:50

问题


I want to locate all the hyperlinks in a webpage which has a xml download link and download them in a loop. There is form which arises when the said hyperlink is clicked, and needs to be filled to proceed with the download. I'm facing issues in the visibility of the elements related to this xml files in the webpage, but I receive the following error:

"selenium.common.exceptions.ElementNotInteractableException: Message: Element is not visible"

I've hereby attached the code, any suggestions to rectify this will be much appreciated.

import os
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

fp = webdriver.FirefoxProfile()

fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.dir", "F:\Projects\Poli_Map\DatG_Py_Dat")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/xml")

driver = webdriver.Firefox(firefox_profile=fp)

m = 'xml'
driver.get('https://data.gov.in/catalog/variety-wise-daily-market-prices-data-cauliflower')
wait = WebDriverWait(driver, 10)

elem = driver.find_element_by_xpath("//*[@href]")
elem.send_keys("xml")
elem.send_keys(Keys.RETURN)

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".list-recent-events li")))

assert m in driver.page_source
for link in elem:
    link.click()


    class FormPage(object):
        def fill_form(self, data):
            driver.execute_script("document.getElementById('edit-download-reasons-non-commercial').click()")
            driver.execute_script("document.getElementById('edit-reasons-d-rd').click()")
            driver.find_element_by_xpath('//input[@name = "name_d"]').send_keys(data['name_d'])
            driver.find_element_by_xpath('//input[@name = "mail_d"]').send_keys(data['mail_d'])
            return self

        def submit(self):
            driver.execute_script("document.getElementById('edit-submit').click()")


    data = {
        'name_d': 'xyz',
        'mail_d': 'xyz@outlook.com',
    }

    time.sleep(3)
    FormPage().fill_form(data).submit()

回答1:


You have to locate only the XML not all hyper links. your locator //*[@href] is locating all the HREF links. Use below code

#locate all the links which have xml 
allelements = driver.find_elements_by_xpath("//a[text()='xml']")

# Iterate all links one by one
for element in allelements:
    element.click()
    class FormPage(object):
        def fill_form(self, data):
            driver.execute_script("document.getElementById('edit-download-reasons-non-commercial').click()")
            driver.execute_script("document.getElementById('edit-reasons-d-rd').click()")
            driver.find_element_by_xpath('//input[@name = "name_d"]').send_keys(data['name_d'])
            driver.find_element_by_xpath('//input[@name = "mail_d"]').send_keys(data['mail_d'])
            return self

        def submit(self):
            driver.execute_script("document.getElementById('edit-submit').click()")


    data = {
        'name_d': 'xyz',
        'mail_d': 'xyz@outlook.com',
    }
    time.sleep(5)
    FormPage().fill_form(data).submit()

    #It opens the download link in new tab So below code again switch back to parent window itself
    window_before = driver.window_handles[0]
    driver.switch_to_window(window_before)


来源:https://stackoverflow.com/questions/46784644/python-selenium-css-selector-element-is-not-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!