Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium:WebElement在登录网站后根本不工作,因为DOM已更新_Python_Selenium_Selenium Webdriver_Selenium Chromedriver - Fatal编程技术网

Python Selenium:WebElement在登录网站后根本不工作,因为DOM已更新

Python Selenium:WebElement在登录网站后根本不工作,因为DOM已更新,python,selenium,selenium-webdriver,selenium-chromedriver,Python,Selenium,Selenium Webdriver,Selenium Chromedriver,一般来说,我对硒和刮削非常陌生,我需要一些帮助。我正试图通过一个使用Selenium提供数据和查询的网站来自动化我的一些工作 问题是,在登录之后,DOM会被刷新,这显然会断开我的WebElement,因为在登录之后,我不再能够执行任何find_元素、get或任何类型的函数。我甚至尝试手动执行此操作,以便不需要任何刷新、等待时间加载等,因为它是在人工监督下进行的:( 如果不使用driver.click()而使用driver.submit() 代码如下: # import web driver fr

一般来说,我对硒和刮削非常陌生,我需要一些帮助。我正试图通过一个使用Selenium提供数据和查询的网站来自动化我的一些工作

问题是,在登录之后,DOM会被刷新,这显然会断开我的WebElement,因为在登录之后,我不再能够执行任何find_元素、get或任何类型的函数。我甚至尝试手动执行此操作,以便不需要任何刷新、等待时间加载等,因为它是在人工监督下进行的:(

如果不使用driver.click()而使用driver.submit()

代码如下:

# import web driver
from selenium import webdriver
import time as time

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

# specifies the path to the chromedriver.exe
driver = webdriver.Chrome('/Users/user/bin/chromedriver')

# driver.get method() will navigate to a page given by the URL address
driver.get('https://app.dnbhoovers.com/')

# locate email form by_class_name
username = driver.find_element_by_xpath('//*[@id="username"]')
# send_keys() to simulate key strokes
username.send_keys(username)

#Sleep some time
time.sleep(0.5)

# locate submit button by_class_name
log_in_button = driver.find_element_by_class_name('continue-btn')

# .click() to mimic button click
log_in_button.click()


#Wait for some seconds as password loads after clicking the contiunue bottom
#try:
driver = WebDriverWait(driver, 5).until(
        EC.presence_of_element_located((By.ID, "password"))
    )
time.sleep(3)


# locate password form by_class_name
password = driver.find_element_by_xpath('//*[@id="password"]')

time.sleep(0.7)
# send_keys() to simulate key strokes
password.send_keys(password)

#Sleep some time
time.sleep(0.7)

# .click() to mimic button click and Sign in
log_in_button.click()
driver.get("http://www.google.com")
AttributeError: 'WebElement' object has no attribute 'get'
登录后,一切都会停止,如果我尝试键入以下函数,则会出现以下错误(即使在等待页面加载之后):

或者,如果我尝试这个简单的任务,我会得到以下结果:

# import web driver
from selenium import webdriver
import time as time

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

# specifies the path to the chromedriver.exe
driver = webdriver.Chrome('/Users/user/bin/chromedriver')

# driver.get method() will navigate to a page given by the URL address
driver.get('https://app.dnbhoovers.com/')

# locate email form by_class_name
username = driver.find_element_by_xpath('//*[@id="username"]')
# send_keys() to simulate key strokes
username.send_keys(username)

#Sleep some time
time.sleep(0.5)

# locate submit button by_class_name
log_in_button = driver.find_element_by_class_name('continue-btn')

# .click() to mimic button click
log_in_button.click()


#Wait for some seconds as password loads after clicking the contiunue bottom
#try:
driver = WebDriverWait(driver, 5).until(
        EC.presence_of_element_located((By.ID, "password"))
    )
time.sleep(3)


# locate password form by_class_name
password = driver.find_element_by_xpath('//*[@id="password"]')

time.sleep(0.7)
# send_keys() to simulate key strokes
password.send_keys(password)

#Sleep some time
time.sleep(0.7)

# .click() to mimic button click and Sign in
log_in_button.click()
driver.get("http://www.google.com")
AttributeError: 'WebElement' object has no attribute 'get'
接下来,如果我尝试刷新或按下键盘,我会得到:

webdriver.ActionChains(driver).send_keys(Keys.DOWN).perform()
AttributeError: 'WebElement' object has no attribute 'w3c'
即使我在.click()命令后添加这几行,告诉驱动程序等待搜索输入出现,也会出现错误

# ... previous code...#
log_in_button.click()

#New lines of code
WebDriverWait(driver, 20).until(
    EC.presence_of_element_located((By.XPATH, "//input[@class='search-query']"))
    )

StaleElementReferenceException: stale element reference: element is not attached to the page document
刷新浏览器也不会有任何区别


如果我检查webiste的元素,在登录后会完全更改,我会看到一些“,我认为问题在于此

driver = WebDriverWait(driver, 5).until(
        EC.presence_of_element_located((By.ID, "password"))
    )
WebDriverWait()返回一个WebElement,您不想这样定义
driver

driver.get("http://www.google.com")
AttributeError: 'WebElement' object has no attribute 'get'

这是在告诉您,
driver
是一个
WebElement
,您不需要它。

请尝试等待,直到可以单击它:

elem = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@class='search-query']")))
elem.click()
尝试2: 如果有正在运行的JQuery,请在单击之前尝试运行此脚本:

wait.until(lambda driver: driver.execute_script('return jQuery.active') == 0)
wait.until(lambda driver: driver.execute_script('return document.readyState') == 'complete')

有iframe元素吗?是的src=“javascript:void(0)”title和style您可能需要切换到iframe。如果没有html代码,很难说。我怎么做?检查@C.Peck的答案。这是绝对正确的。谢谢!但它也不起作用。如果我删除“driver=”到那一行,我仍然没有得到StaleElement错误。谢谢你的回答!!但不幸的是,错误仍然是一样的。显然没有webdriver,因为我得到了TimeoutException。wait.until代码不起作用,所以我尝试了“elem=WebDriverWait(driver,10)。直到(”我仍然没有得到任何结果。我也在尝试切换iframe,但我得到AttributeError:“WebElement”对象没有属性“switch_to”新页面是否有不同的域?我的意思是页面url的第一部分不同吗?url相同,但DOM完全改变。我又添加了一个选项,但没有页面源,很难判断