英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论