Problem with web scraping with selenium in python

I have written code to log in to a website with Python. But it takes very long to implement and does not lead to what is expected and gives a message saying “DevTools listening on”. (The send_keys command doesn’t work and will not write “password” and “username” to input boxes).

The code is:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from selenium import webdriver
from selenium.webdriver.common.by import By
import time
username="rahbar"
password="password"
print("something before scrape")
driver = webdriver.Chrome()
driver.get("https://plp.irbroker.com/index.do")
time.sleep(100) #waits 100 seconds
driver.maximize_window()
driver.find_element("name", "j_username").send_keys(username)
driver.find_element_by_name( name="j_password" ).send_keys(password)
print("something after scrape")
</code>
<code>from selenium import webdriver from selenium.webdriver.common.by import By import time username="rahbar" password="password" print("something before scrape") driver = webdriver.Chrome() driver.get("https://plp.irbroker.com/index.do") time.sleep(100) #waits 100 seconds driver.maximize_window() driver.find_element("name", "j_username").send_keys(username) driver.find_element_by_name( name="j_password" ).send_keys(password) print("something after scrape") </code>
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
username="rahbar"
password="password"
print("something before scrape")
driver = webdriver.Chrome()

driver.get("https://plp.irbroker.com/index.do")
time.sleep(100) #waits 100 seconds
driver.maximize_window()
driver.find_element("name", "j_username").send_keys(username)

driver.find_element_by_name( name="j_password" ).send_keys(password)
print("something after scrape")

The message gotten in the terminal is

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>something before scrape
*DevTools listening on ws://127.0.0.1:57315/devtools/browser/fd73344f-c857-439e-b20c-c0e76d39f389
Created TensorFlow Lite XNNPACK delegate for CPU.
Attempting to use a delegate that only supports static-sized tensors with a graph that has dynamic-sized tensors (tensor#58 is a dynamic-sized tensor)*
</code>
<code>something before scrape *DevTools listening on ws://127.0.0.1:57315/devtools/browser/fd73344f-c857-439e-b20c-c0e76d39f389 Created TensorFlow Lite XNNPACK delegate for CPU. Attempting to use a delegate that only supports static-sized tensors with a graph that has dynamic-sized tensors (tensor#58 is a dynamic-sized tensor)* </code>
something before scrape
*DevTools listening on ws://127.0.0.1:57315/devtools/browser/fd73344f-c857-439e-b20c-c0e76d39f389
Created TensorFlow Lite XNNPACK delegate for CPU.
Attempting to use a delegate that only supports static-sized tensors with a graph that has dynamic-sized tensors (tensor#58 is a dynamic-sized tensor)*

I expect the code to open the webpage and write “password” and “username” to input boxes in a reasonable short time period.
Can you guide me on this issue (I am new to Python, so sorry if my question is trivial)?

New contributor

Soroush Kalantari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

  • Don’t rely on time.sleep(100) which may cause long delays.
  • Instead,replace the delay with WebDriverWait to wait for elements to load properly.
  • Here is the update version of code:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import time
username = "rahbar"
password = "password"
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://plp.irbroker.com/index.do")
try:
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, "j_username"))
)
driver.find_element(By.NAME, "j_username").send_keys(username)
driver.find_element(By.NAME, "j_password").send_keys(password)
driver.find_element(By.XPATH, "//button[@type='submit']").click()
print("Login fields populated successfully.")
except Exception as e:
print(f"Error occurred: {e}")
finally:
time.sleep(10)
driver.quit()
</code>
<code>from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options import time username = "rahbar" password = "password" chrome_options = Options() chrome_options.add_argument("--start-maximized") driver = webdriver.Chrome(options=chrome_options) driver.get("https://plp.irbroker.com/index.do") try: WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.NAME, "j_username")) ) driver.find_element(By.NAME, "j_username").send_keys(username) driver.find_element(By.NAME, "j_password").send_keys(password) driver.find_element(By.XPATH, "//button[@type='submit']").click() print("Login fields populated successfully.") except Exception as e: print(f"Error occurred: {e}") finally: time.sleep(10) driver.quit() </code>
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import time

username = "rahbar"
password = "password"
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://plp.irbroker.com/index.do")
try:
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.NAME, "j_username"))
    )
    driver.find_element(By.NAME, "j_username").send_keys(username)
    driver.find_element(By.NAME, "j_password").send_keys(password)
    driver.find_element(By.XPATH, "//button[@type='submit']").click()
    print("Login fields populated successfully.")
    
except Exception as e:
    print(f"Error occurred: {e}")
finally:
    time.sleep(10)
    driver.quit()

1

Let’s improve the code before the actual scraping.

It is recommended that all data be moved to the beginning of the code. It is easier to see all the necessary data in one place:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># data for log-in
username = "rahbar"
password = "password"
site_url = "https://plp.irbroker.com/index.do"
</code>
<code># data for log-in username = "rahbar" password = "password" site_url = "https://plp.irbroker.com/index.do" </code>
# data for log-in
username = "rahbar"
password = "password"
site_url = "https://plp.irbroker.com/index.do"

Now let’s concentrate on optimizing and refining a few points.

First, let’s get website login and browser initialization out of the way and add fullscreen mode to the browser startup parameters:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># initialize driver
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
driver = webdriver.Chrome(options=chrome_options)
driver.get(site_url)
</code>
<code># initialize driver chrome_options = Options() chrome_options.add_argument("--start-maximized") driver = webdriver.Chrome(options=chrome_options) driver.get(site_url) </code>
# initialize driver
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
driver = webdriver.Chrome(options=chrome_options)
driver.get(site_url) 

Now, instead of time.sleep(),let’s implement a method that returns the element after it has been displayed on the site, although it is not always accurate and correct:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># wait until both input elements will be on screen
username_element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='j_username']")))
password_element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='j_password']")))
</code>
<code># wait until both input elements will be on screen username_element = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='j_username']"))) password_element = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='j_password']"))) </code>
# wait until both input elements will be on screen
username_element = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='j_username']")))
password_element = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='j_password']")))

If you are entering data into an input element, it is better to use EC.element_to_be_clickable. This method checks not only for the presence of the element on the page, but also for interactivity(i.e.,whether the element is visible or disabled).

Now, let’s clear the input field (if necessary) and enter the value:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># clear them (not important is site does not use start value)
username_element.clear()
password_element.clear()
# set values
username_element.send_keys(username)
password_element.send_keys(password)
</code>
<code># clear them (not important is site does not use start value) username_element.clear() password_element.clear() # set values username_element.send_keys(username) password_element.send_keys(password) </code>
# clear them (not important is site does not use start value)
username_element.clear()
password_element.clear()
# set values
username_element.send_keys(username)
password_element.send_keys(password)

And finally, we move on to the last part (optional since it wasn’t mentioned in the question) now we wait until the login button can be clicked (this works the same as with the input field):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># wait until login button will be clickable and then press it
log_in = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()
</code>
<code># wait until login button will be clickable and then press it log_in = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click() </code>
# wait until login button will be clickable and then press it
log_in = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()

Now the code is optimized and clear!

Here is the full version:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
# data for log-in
username = "rahbar"
password = "password"
site_url = "https://plp.irbroker.com/index.do"
# something before scrape
# initialize driver
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
driver = webdriver.Chrome(options=chrome_options)
driver.get(site_url)
# wait until both input elements will be on screen
username_element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='j_username']")))
password_element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='j_password']")))
# clear them (not important is site does not use start value)
username_element.clear()
password_element.clear()
# set values
username_element.send_keys(username)
password_element.send_keys(password)
# wait until login button will be clickable and then press it
log_in = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()
# and then thing after scrape, good luck with scrape btw
</code>
<code>from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options # data for log-in username = "rahbar" password = "password" site_url = "https://plp.irbroker.com/index.do" # something before scrape # initialize driver chrome_options = Options() chrome_options.add_argument("--start-maximized") driver = webdriver.Chrome(options=chrome_options) driver.get(site_url) # wait until both input elements will be on screen username_element = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='j_username']"))) password_element = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='j_password']"))) # clear them (not important is site does not use start value) username_element.clear() password_element.clear() # set values username_element.send_keys(username) password_element.send_keys(password) # wait until login button will be clickable and then press it log_in = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click() # and then thing after scrape, good luck with scrape btw </code>
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

# data for log-in
username = "rahbar"
password = "password"
site_url = "https://plp.irbroker.com/index.do"

# something before scrape

# initialize driver
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
driver = webdriver.Chrome(options=chrome_options)
driver.get(site_url)

# wait until both input elements will be on screen
username_element = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='j_username']")))
password_element = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='j_password']")))
# clear them (not important is site does not use start value)
username_element.clear()
password_element.clear()
# set values
username_element.send_keys(username)
password_element.send_keys(password)
# wait until login button will be clickable and then press it
log_in = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()

# and then thing after scrape, good luck with scrape btw

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật