我正在尝试从网站上爬取图像,使用了Selenium,但在代码中出现了错误。

huangapple go评论83阅读模式
英文:

I am trying to scrape images from website and using selenium but getting an error in the code

问题

Your code appears to have a JavaScript error that is causing the "Cannot read properties of undefined (reading 'click')" message. The issue seems to be related to the line where you are trying to execute a click action on an element with the class "accordion-toggle."

A potential solution would be to check if the element with the class "accordion-toggle" exists before attempting to click it. You can use a condition like this:

element = driver.find_element(By.CLASS_NAME, 'accordion-toggle')
if element:
    element.click()
    sleep(10)
else:
    # Handle the case where the element is not found
    print("Element with class 'accordion-toggle' not found")

# Continue with the rest of your code

This code checks if the element exists before attempting to click it, which should help prevent the "Cannot read properties of undefined" error.

Make sure you also have the necessary imports for Selenium and other libraries at the beginning of your code.

英文:

My code

for idx, row in tqdm(df.iterrows(), total=df.shape[0]):
    imgs = []
    try:
        driver.get(row['listing_url'])
        sleep(10)
        script = "document.getElementsByClassName('accordion-toggle')[0].click();"
        driver.execute_script(script)
        sleep(10)
        scroll_down(driver)
        imgs = driver.find_elements(By.XPATH, '//img[@class="i1o0kbi8 i1mla2as i1cqnm0r dir dir-ltr"]')
        imgs = list(set([img.get_attribute('src') for img in imgs]))
    finally:
        os.makedirs('images', exist_ok=True)
        with open(f'images/{row["id"]}_{row["host_id"]}.json', 'w') as f:
            json.dump(imgs, f)

Error

>JavascriptException: Message: javascript error: Cannot read properties of undefined (reading 'click')

What is wrong with my code and what is the correct approach

答案1

得分: 1

This error message...

JavascriptException: Message: javascript error: Cannot read properties of undefined (reading 'click')

...implies that click() method can't be invoked on undefined element.


This usecase

Possibly the following part of the script:

document.getElementsByClassName('accordion-toggle')[0]

doesn't identify any element, so remains undefined and click() method can't be invoked on it.


Solution

Try to identify the desired element uniquely within the HTML DOM using a suitable locator strategy and execute your code.

英文:

This error message...

JavascriptException: Message: javascript error: Cannot read properties of undefined (reading 'click')

...implies that click() method can't be invoked on undefined element.


This usecase

Possibly the following part of the script:

document.getElementsByClassName('accordion-toggle')[0]

doesn't identifies any element, so remains undefined and click() method can't be invoked on it.


Solution

Try to identify the desired element uniquely within the HTML DOM using a suitable locator strategy and execute your code.

huangapple
  • 本文由 发表于 2023年6月13日 04:06:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459986.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定