【问题标题】:Selenium Webdriver-python, unable to find the dynamic element, tried all the possible waysSelenium Webdriver-python,找不到动态元素,尝试了所有可能的方法
【发布时间】:2018-07-31 22:06:37
【问题描述】:

登录后,我可以成功导航到所需的页面。静态元素被定位和打印,和平。页面调用 JavaScript,内容在大约 4-5 秒后更新,我无法找到动态元素。

我在加载 Javascript 元素之前和之后附加了 Inspect 元素的图像。

请查看下面的代码并提出可能的解决方案。 附言在 100 次中,这工作了大约 2-3 次。

layer = "https://desired.website"
driver.get(layer)
driver.find_element_by_id("email").send_keys('my@email.com')
driver.find_element_by_id("password").send_keys('myPassword')
driver.find_element_by_class_name("css-173kae7").click()

#NOW I'M SUCCESSFULLY LOGGED IN
#Opening the Desired Page, This is a static element
wait = WebDriverWait(driver, 30)
element = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "css-1nga9gi")))
driver.execute_script('''window.open("desired.page","_blank");''')

#Successfully opened desired page and switched to newly opened tab
#Trying to access the element present in <tbody> tag, please refer "Inspect Element after JavaScript elements are loaded"- image.
wait = WebDriverWait(driver, 30)
element = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "css-167dqxg")))
check = driver.find_elements_by_class_name("ccss-13ozsj7")
for e in check:
    print(e.text)
print("ALL DATA SUCCESSFULLY PRINTED")

30 秒内没有任何反应,我收到超时错误,并显示“所有数据已成功打印”。

我得到的错误代码是:

element = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "css-167dqxg")))
File "C:\Python\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

请查看 Inspect 元素。

Inspect Element at the beginning of Page Load

Inspect Element after JavaScript elements are loaded

【问题讨论】:

  • 我看不出您的代码有任何明显的问题,您可能希望使用 page_source() 检查 selemium 看到的页面源,以验证它甚至可以看到它。
  • 页面源返回简单的 HTML 内容,没有任何有用的数据,因为页面是 JS 渲染的。似乎找不到,此代码或网站有什么问题。是否有任何替代方法来抓取数据,其中包含在网站中导航的登录设施?
  • 嗯是您试图在页面源输出中定位的标签吗?你没头没脑的跑吗?当 selenium 运行时,你能看到你试图用它定位的元素吗?我见过一些代码不能很好地与 phantomjs 一起使用,但在 chrome 中可以正常工作的情况。
  • 您可能在切换到新标签的新内容时遇到问题,您是如何切换到新标签的?
  • @Sourav 使用基于文本的 HTML 元素更新问题 (By.CLASS_NAME, "css-167dqxg")

标签: javascript python-3.x selenium testing selenium-webdriver


【解决方案1】:

您可能没有切换到打开的选项卡上的新内容,请尝试添加:

#after this line
driver.execute_script('''window.open("desired.page","_blank");''')
#try adding this
#this will switch to the newest tab
driver.switch_to_window(driver.window_handles[-1])

您可能无法在旧标签上寻找所需的元素

【讨论】:

    【解决方案2】:

    当使用 Python 时,使用“starts-with”而不是“undefined-object”

    电子邮件地址 XPATH .//*[@id='email-undefined-objectObject-26149']

    driver.find_element_by_xpath(".//input[starts-with(@id,'email')]".clear() driver.find_element_by_xpath(".//input[starts-with(@id,'email')]".send_keys("scotty.mitch@gmail.com")) ....或“包含” driver.find_element_by_xpath(".//input[contains(@id,'email')]".clear()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      相关资源
      最近更新 更多