无法找到硒中的元素:{“ method”:“ css选择器”,“ selector”:“ [id =” identifierId“]”}

时间:2019-10-09 03:59:48

标签: python selenium automation selenium-chromedriver

我想通过电子邮件自动下载到本地并发送报告(仪表板数据工作室),但是当我在数据工作室中运行代码时,chrome驱动程序打开,并显示通知“此时无法查看报告或没有报告访问”,我该怎么办?这是我的代码:

# login to datastudio & refresh the newest data
self.chrome_driver.get(self.url)  
self.chrome_driver.find_element_by_id("identifierId").send_keys(self.email + u'\ue007')
self.quick_wait.until(EC.element_to_be_clickable((By.NAME, "password"))).send_keys(self.password+u'\ue007')
self.quick_wait.until(EC.element_to_be_clickable((By.XPATH, self.report_refresh))).click() # refresh report data
time.sleep(15)
# download file to local path
try:
    ActionChains(self.chrome_driver).context_click(self.chrome_driver.find_element_by_xpath(self.product_export)).perform()
    self.quick_wait.until(EC.element_to_be_clickable((By.XPATH, self.product_download))).click()
    time.sleep(20)
    self.quick_wait.until(EC.element_to_be_clickable((By.ID, self.report_download))).click()
    self.chrome_driver.find_element_by_css_selector(self.report_to_pdf).click()
    time.sleep(20)
except AssertionError as error:
    print(error)
    self.chrome_driver.quit()

我收到错误消息,例如:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="identifierId"]"}

enter image description here

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

页面加载可能是一个问题。尝试添加隐式或显式等待。

1。应用所需的等待(隐式或显式等待)

隐含等待

driver.implicitly_wait(15)

明确等待:

wait = WebDriverWait(driver, 10)
wait.until(ec.visibility_of_element_located((By.XPATH, "//*[contains(@class,'result-list-entry')]")))

示例:

self.chrome_driver.implicitly_wait(15)
self.chrome_driver.get(self.url)
self.quick_wait.until(EC.element_to_be_clickable((By.ID, "identifierId")))
self.chrome_driver.find_element_by_id("identifierId").send_keys(self.email + u'\ue007')
self.quick_wait.until(EC.element_to_be_clickable((By.NAME, "password"))).send_keys(self.password+u'\ue007')

2。检查element是否是iframe元素的子元素。如果是这样,请切换到iframe并尝试点击