Home > OS >  Inconsistently getting "Exception has occurred: JavascriptException" error selenium
Inconsistently getting "Exception has occurred: JavascriptException" error selenium

Time:09-09

Hello something really weird happening I'm getting "Exception has occurred: JavascriptException / Message: javascript error: this.each is not a function" on this specific line:

waiting.until(EC.visibility_of_element_located((By.CLASS_NAME, "xxxx")))

it does work half of the time but at the other half gives this error

Code:

from selenium import webdriver
import time
import requests
from cgitb import text
from unittest import result
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver.get("https://steamcommunity.com/market/listings/730/M4A4 | Mainframe (Minimal Wear)")

waiting = WebDriverWait(driver, 10)
waiting.until(EC.visibility_of_element_located((By.CLASS_NAME, "csgofloat-itemfloat")))

page_source = driver.page_source
soup = BeautifulSoup(page_source, "html.parser")
hopefully_floats = soup.find_all("div", {"class": "csgofloat-itemfloat"})

time.sleep(15)
print(hopefully_floats)
print(type(hopefully_floats))
driver.quit()

CodePudding user response:

This is one way to achieve your end goal (or what I understand from it). First, make sure that extension is installed, and check its exact location. Then you can do something like this:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


import time as t

chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument('--load-extension=/home/user/.config/chromium/Default/Extensions/jjicbefpemnphinccgikpdaagjebbnhg/2.4.3_0') ## extension location on your filesystem

chrome_options.add_argument("window-size=1280,720")

webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
wait = WebDriverWait(browser, 20)
url = 'https://steamcommunity.com/market/listings/730/M4A4 | Mainframe (Minimal Wear)'
browser.get(url)
t.sleep(2)
float_vals = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'div[]')))
for f in float_vals:
    float_value = f.find_element(By.CSS_SELECTOR, 'div[]')
    paint_seed = f.find_element(By.CSS_SELECTOR, 'div[]')
    print(float_value.text, paint_seed.text)

Result:

Float: 0.12355003505945 Paint Seed: 151
Float: 0.11305310577154 Paint Seed: 955
Float: 0.14578416943550 Paint Seed: 314
Float: 0.09940030425787 Paint Seed: 938
Float: 0.12989912927151 Paint Seed: 328
Float: 0.12652109563351 Paint Seed: 835
Float: 0.14991194009781 Paint Seed: 525
Float: 0.14711093902588 Paint Seed: 122
Float: 0.11300826072693 Paint Seed: 351
Float: 0.13550239801407 Paint Seed: 485

Selenium documentation: https://www.selenium.dev/documentation/

CodePudding user response:

You are missing a driver initialization, something like

driver = webdriver.Chrome(service=webdriver_service, options=options)

And opening a link, something like

driver.get(url)
  • Related