Selenium即使在Ajax页面中显式等待后也找不到元素 [英] Selenium can't find element even after explicit waiting in an Ajax Page

查看:77
本文介绍了Selenium即使在Ajax页面中显式等待后也找不到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动更新Web应用程序中的字段.因此,登录后

I'm trying to automate updating fields in a web application. Thus the url does not change after logging in

到目前为止,这是我的代码

Here is my code so far

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

path_to_chromedriver = "C:/chromedriver"
browser = webdriver.Chrome(executable_path=path_to_chromedriver)

"""Login Page"""
login_url = "url"
browser.get(login_url)

username = browser.find_element_by_id("username")
password = browser.find_element_by_id("password")

username.send_keys("username")
password.send_keys("password")

browser.find_element_by_name("submit").click()

"""Application front page"""
searchBar = browser.find_element_by_id("searchBar")
searchBar.send_keys("item to be searched")

button = browser.find_element_by_id("searchButton")
button.click()

"""Click on item on search results"""
#starting here, everything doesn't work
wait = WebDriverWait(browser,10)
item = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#item')))
item.click()#this never works as it just times out

此站点是一个Web应用程序.每次单击后,我都会打印出页面源,并且在首页之后它不会更改,但是在Chrome浏览器中确实会更改.显式和隐式等待都不起作用.有什么建议吗?

This site is a web application. I've printed out the page source after each click and it doesn't change after the homepage however in the Chrome browser it does change. Explicit and Implicit waits both do not work. Any suggestions?

预先感谢

-编辑-

我有点犹豫要发布html,因为它是一个自定义Web应用程序.但是,主体的类别为"dhtmlx_winviewport",Web应用程序中发生更改的部分以类似

I'm a bit hesitant to post the html because it's a custom web app. However, the class of the body is "dhtmlx_winviewport" and the part of the web app that did change starts out with something like

<iframe id = "frameID" name = "frame1" src="some link that shows the item I searched for" height="400" width="400" frameboorder="0" style="z-index: 10; position: absolute; visibility: visible; width:400px; height:800px;"> == $0

我要单击的内容是表格中的单元格

The thing I want to click on is a cell in a table

<td align="left" valign="middle" title="title">title</td>


我得到的错误是


The error I am getting is

回溯(最近通话最近):文件"C:\ script.py",第45行,在item = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'css')))文件"C:\ Users \ me \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \ support \ wait.py",第80行,直到引发TimeoutException(消息,屏幕,堆栈跟踪)selenium.common.exceptions.TimeoutException:消息:

Traceback (most recent call last): File "C:\script.py", line 45, in item = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'css'))) File "C:\Users\me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:

推荐答案

Wait的对象实例在哪里,是否不在此代码块中?例如我的是-

Where is your object instantiation for Wait, is it not in this block of code? For example mine is -

self._wait = WebDriverWait(driver, 15)

您也可以尝试使用元素可点击",而不是存在元素"

You might also try using 'element to be clickable' rather than 'presence of element located'

item = self._wait.until(EC.element_to_be_clickable((By.XPATH,"//MY_XPATH_IS_HERE")))

您收到任何错误消息吗?item == None是否返回true?

Are you getting any error messages? Does item==None return true?

这篇关于Selenium即使在Ajax页面中显式等待后也找不到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆