Python Selenium CSS选择器:元素不可见
我想找到一个有xml下载链接的网页中的所有超链接,并在一个循环中下载它们。 点击所述超链接时会出现一个表单,需要填写该表单才能继续下载。 我在网页中遇到与此xml文件相关的元素的可见性问题,但我收到以下错误: “selenium.common.exceptions.ElementNotInteractiableException:消息: “元素不可见” 我特此附上守则,如有任何修改建议,将不胜感激Python Selenium CSS选择器:元素不可见,python,css,selenium,webdriver,visibility,Python,Css,Selenium,Webdriver,Visibility,我想找到一个有xml下载链接的网页中的所有超链接,并在一个循环中下载它们。 点击所述超链接时会出现一个表单,需要填写该表单才能继续下载。 我在网页中遇到与此xml文件相关的元素的可见性问题,但我收到以下错误: “selenium.common.exceptions.ElementNotInteractiableException:消息: “元素不可见” 我特此附上守则,如有任何修改建议,将不胜感激 import os import time from selenium import webdri
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()
您必须只定位XML,而不是所有的超链接。您的定位器正在查找所有href链接。使用下面的代码
#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)
您必须只定位XML,而不是所有的超链接。您的定位器正在查找所有href链接。使用下面的代码
#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)
谢谢纳伦德拉的回复。运行和第一次下载后,我面临以下错误:selenium.common.exceptions.StaleElementReferenceException:消息:stale的元素引用:元素不再附加到DOM或页面已刷新感谢您的回复。在第一次下载之后,运行时会遇到以下错误:selenium.common.exceptions.StaleElementReferenceException:Message:stale的元素引用:元素不再附加到DOM或页面已刷新