1. ホーム
  2. python

[解決済み] AttributeError: 'WebDriver' オブジェクトには 'findElement' 属性がありません。

2022-03-04 11:13:27

質問

私はpythonとseleniumで作業しています。私は、ページ内のすべての要素を抽出したいのですが、それはクラスやIDではなく、スタイルを持っています。 私は次のコードを使用しています。

driver.findElement("By.xpath(//div[style='background: rgb(255, 255, 255); border-radius: 1rem; padding: 0px 1.25rem; margin-top: 1.25rem; margin-bottom: 3rem;']")

しかし、これは私にエラーを与える。

AttributeError: 'WebDriver' object has no attribute 'findElement'

以下はソースコードです。

<div style="background: rgb(255, 255, 255); border-radius: 1rem; padding: 0px 1.25rem; margin-top: 1.25rem; margin-bottom: 3rem;"><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/43.png" style="width: 24px; height: 24px; margin-right: 15px;">ახალი ხორცი და ხორცპროდუქტი<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/13.png" style="width: 24px; height: 24px; margin-right: 15px;">ახალი ბოსტნეული<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/12.png" style="width: 24px; height: 24px; margin-right: 15px;">ახალი ხილი<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/38.png" style="width: 24px; height: 24px; margin-right: 15px;">რძის ნაწარმი და კვერცხი<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/26.png" style="width: 24px; height: 24px; margin-right: 15px;">ყველი<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/30.png" style="width: 24px; height: 24px; margin-right: 15px;">პური<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/253.png" style="width: 24px; height: 24px; margin-right: 15px;">საკონდიტრო<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/41.png" style="width: 24px; height: 24px; margin-right: 15px;">მზა კერძები<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/591.png" style="width: 24px; height: 24px; margin-right: 15px;">გუდვილის რეცეპტები<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/33.png" style="width: 24px; height: 24px; margin-right: 15px;">ახალი თევზი და ზღვის პროდუქტი<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/32.png" style="width: 24px; height: 24px; margin-right: 15px;">ბაკალეა<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/27.png" style="width: 24px; height: 24px; margin-right: 15px;">ტკბილეული და სასუსნავი<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/31.png" style="width: 24px; height: 24px; margin-right: 15px;">გაყინული პროდუქტი<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/29.png" style="width: 24px; height: 24px; margin-right: 15px;">უალკოჰოლო სასმელები<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/34.png" style="width: 24px; height: 24px; margin-right: 15px;">ალკოჰოლური სასმელები<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/308.png" style="width: 24px; height: 24px; margin-right: 15px;">საკანცელარიო და სათამაშოები<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/42.png" style="width: 24px; height: 24px; margin-right: 15px;">ბავშვთა კვება და მოვლა<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/24.png" style="width: 24px; height: 24px; margin-right: 15px;">ჰიგიენა<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/28.png" style="width: 24px; height: 24px; margin-right: 15px;">სახლის მოვლა<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/25.png" style="width: 24px; height: 24px; margin-right: 15px;">ცხოველთა&nbsp;კვება&nbsp;და&nbsp;მოვლა<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/40.png" style="width: 24px; height: 24px; margin-right: 15px;">ელექტრო ნივთები<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/37.png" style="width: 24px; height: 24px; margin-right: 15px;">სადღესასწაულო ნივთები<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p><p style="margin: 0px; padding: 1.125rem 0px; font-size: 0.875rem; display: flex; align-items: center; border-bottom: 1px solid rgb(241, 241, 245);"><img src="/icons/shop/5/447.png" style="width: 24px; height: 24px; margin-right: 15px;">Home Cooking Adventures - რეცეპტები<span style="margin-right: 0.5rem; font-size: 1rem; margin-left: auto;"><i class="icon-chevron-right" style="font-size: 0.75rem; color: rgb(0, 150, 108);"></i></span> </p></div>

以下は、ページのリンクと、クリックしたい要素です。 https://moitane.ge/shop/5-gudvili/43-axali-xorci-da-xorcproduqti 画像の説明をここに入力

解決方法

pythonのselenium webriverには、以下のものがありません。 findElement メソッドを使用します。

を持っています。

 driver.find_element(By.XPATH, 'your xpath')

ですべてのメソッドを確認してみてください。

print(dir(driver))

を試してみてください。

import time

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://moitane.ge/shop/5-gudvili/43-axali-xorci-da-xorcproduqti")
time.sleep(3)
el = driver.find_element(By.XPATH, "//div[@class='style__ShopProductSubCategoryChip-sc-1bc3ssb-2 iKSeHs']")
el.click()
time.sleep(3)
driver.quit()