gpt4 book ai didi

python - 如何定位在python selenium中单击按钮后出现的网页上的元素

转载 作者:行者123 更新时间:2023-12-05 04:41:28 32 4
gpt4 key购买 nike

我是一名新的 Python 程序员。最近,我开始从事自动化/抓取工作。目前,我遇到了点击按钮后出现页面的情况,但我无法在该页面上找到元素。

from selenium import webdriver

from selenium.webdriver.support.ui import Select

import time


driver = webdriver.Chrome(r"/Users/prateep/Downloads/chromedriver")

driver.get("https://example.com")

driver.maximize_window()

time.sleep(10)

button = driver.find_element_by_class_name("a-IRR-buttons")

button.click()

time.sleep(5)

driver.find_element("P52_NAME").send_keys("Test!!!")

selectStatus = Select(driver.find_element("P52_ASSESSMENT_STATUS_ID"))

selectStatus.select_by_value("O - Open")

driver.close()

输出:-

driver.find_element_by_name("P52_NAME").send_keys("Test!!!")
Traceback (most recent call last):
File "/Users/prateep/Aegis_Automation/Aegis_Automation.py", line 25, in <module>
driver.find_element_by_name("P52_NAME").send_keys("Test!!!")
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 658, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 1238, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 418, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 243, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="P52_NAME"]"}

最佳答案

问题出在这一行

driver.find_element("P52_NAME").send_keys("Test!!!")

您使用的 find_element 有误。

让我们看看 find_element 内部有什么

def find_element(self, by=By.ID, value=None):
"""
Find an element given a By strategy and locator. Prefer the find_element_by_* methods when
possible.

:Usage:
element = driver.find_element(By.ID, 'foo')

:rtype: WebElement
"""
if self.w3c:
if by == By.ID:
by = By.CSS_SELECTOR
value = '[id="%s"]' % value
elif by == By.TAG_NAME:
by = By.CSS_SELECTOR
elif by == By.CLASS_NAME:
by = By.CSS_SELECTOR
value = ".%s" % value
elif by == By.NAME:
by = By.CSS_SELECTOR
value = '[name="%s"]' % value
return self.execute(Command.FIND_ELEMENT, {
'using': by,
'value': value})['value']

如你所见,我们必须将它与

  1. By.ID
  2. By.CLASS_NAME
  3. 作者:TAG_NAME
  4. By.LINK_TEXT
  5. By.PARTIAL_LINK_TEXT
  6. By.NAME
  7. By.CSS_SELECTOR
  8. By.XPATH

如果 P52_NAME 是名称并且它是唯一的并且代表 HTMLDOM 中所需的元素,那么理想情况下您应该使用

driver.find_element(By.NAME, "P52_NAME").send_keys("Test!!!")

如果我们在 HTML DOM 中有unique条目,请检查开发工具(Google chrome)。

检查步骤:

在 Chrome 中按 F12 -> 转到 element 部分 -> 执行 CTRL + F -> 然后粘贴 NAME 并查看您想要的 元素 是否通过 1/1 匹配节点得到突出显示

更新:

wait = WebDriverWait(driver, 30)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "iframe xpath")))
wait.until(EC.visibility_of_element_located((By.ID, ""))).send_keys("")

导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

关于python - 如何定位在python selenium中单击按钮后出现的网页上的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70067869/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com