Issue
So, the last 3 hours or so I have tried to get Selenium to work without success. I managed to make it work with requests and Beautifulsoup, but apparently site uses javascript to load data after login so I cannot scrape the data I want after successful login.
Below is the script I am trying to work with.
``
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
driver = webdriver.Chrome("/home/user/Desktop/chromedriver")
username = "FakeUsername"
password = "PasswordFake"
driver.get("https://www.helen.fi/kirjautuminen")
time.sleep(10)
# find username/email field and send the username itself to the input field
# find password input field and insert password as well
driver.find_element(By.XPATH,'//input[@id="username"]').send_Keys(username)
# click login button
``
(Yes, I know its missing password and submit actions, but I can't get it to write anything into username or password input boxes. Also same script seems to be working fine with github's login page, so I really can't understand what I am doing wrong here).
After running the script, chrome opens, site loads fine, but for some reason I get error
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@id="username"]"}
I have tried with element ID and Name with similar errors, except it said something about "unable to locate element: css selector ..."
If anyone has some advice to give a newbie, it would be awesome. This is starting to give me headache.
I except the script to write username into username input box, but nothing happens.
Solution
this occurs because there is an iframe in the page code. It is necessary to switch to the iframe and then search for the element, follow the code with the correction.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path= "chromedriver.exe")
username = "FakeUsername"
password = "PasswordFake"
driver.get("https://www.helen.fi/kirjautuminen")
# find username/email field and send the username itself to the input field
# find password input field and insert password as well
iframe = driver.find_element(By.XPATH, '//iframe[@class="login-iframe"]')
driver.switch_to.frame(iframe)
driver.find_element(By.XPATH,'//input[@id="username"]').send_keys(username)
# click login button
Answered By - Gabriel Passos
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.