I'm very new to Selenium and scraping in general and I need some help please. I'm trying to automate some of my work through a website that provides data and queries using Selenium.
The problem is that after the Login, the DOM gets refreshed and apparently that disconects my WebElement, as I no longer am able of doing any find_element, get or any type of function after the login is done. I am even trying to do it manually so that any refresh, wait time load, etc. is not needed because it is under human supervision :(
The same issues happens if instead of doing driver.click() I use driver.submit()
The code is as following:
# 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()
After the login is where everything goes down, and if I try to type the following functions I get these errors (even after waiting for the page to load):
search = driver.find_element_by_xpath("//input[@class='search-query']")
StaleElementReferenceException: stale element reference: element is not attached to the page document
Or if I even try this simple task I get the following:
driver.get("http://www.google.com")
AttributeError: 'WebElement' object has no attribute 'get'
And this goes on, if I try to refresh or go down with the keyboard I get:
webdriver.ActionChains(driver).send_keys(Keys.DOWN).perform()
AttributeError: 'WebElement' object has no attribute 'w3c'
Even if I add this lines after the .click() command to tell the driver to wait for the search input to appear, I get errors
# ... 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
Refreshing the browser will not make any difference either.
If I inspect the element of the webiste, after the login the changes completely and I see some ""<meta name="robots" content="noindex, nofollow>"" also.
I'm really stuck right now so any help is deeply appreciated!
Thanks for your time and for reading this long post! You can have a potato :)