使用Selenium定位元素

问题描述 投票:0回答:1

使用本网站: (https://buyee.jp/mercari/search?keyword=seiko%20S-212&status=on_sale) 或查看下面的 html:

HTML with Element of Interest

我正在尝试找到具有值:“Items 1 to 2”的元素,请参见下面的屏幕截图:

我尝试通过xpath定位元素,我的完整代码:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

val = 'https://buyee.jp/mercari/search?keyword=seiko%20S-212&status=on_sale'
driver = webdriver.Firefox()
driver.get(val)
wait = WebDriverWait(driver, 10)

items = driver.find_element(By.XPATH, '/html/body/div/div/div/div/div[2]/div[2]')

但是我收到以下错误:

Traceback (most recent call last):
  File "/home/kali/PycharmProjects/pythonProject/auto.py", line 10, in <module>
    items = driver.find_element(By.XPATH, '/html/body/div/div/div/div/div[2]/div[2]')
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/kali/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/selenium/webdriver/remote/webdriver.py", line 741, in find_element
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/kali/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/selenium/webdriver/remote/webdriver.py", line 347, in execute
    self.error_handler.check_response(response)
  File "/home/kali/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: /html/body/div/div/div/div/div[2]/div[2]; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Stacktrace:
RemoteError@chrome://remote/content/shared/RemoteError.sys.mjs:8:8
WebDriverError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:187:5
NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:505:5
element.find/</<@chrome://remote/content/marionette/element.sys.mjs:135:16


Process finished with exit code 1
python selenium-webdriver web-scraping
1个回答
0
投票

您的页面使用 iframe。

所以,这是一个具有有效 XPath 的工作代码:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

val = 'https://buyee.jp/mercari/search?keyword=seiko%20S-212&status=on_sale'
driver = webdriver.Firefox()
driver.get(val)
wait = WebDriverWait(driver, 10)

iframe = driver.find_element(By.ID, "search_result_iframe")
driver.switch_to.frame(iframe) # mandatory to change the context

# need to be improved:
wait = WebDriverWait(driver, 10)
import time
time.sleep(10) 
  
item = driver.find_element(By.XPATH, '//div[text()="Items 1 to 2"]')
print(item.text)

driver.switch_to.default_content()
driver.quit()
© www.soinside.com 2019 - 2024. All rights reserved.