【问题标题】:Confusion with selenium locator not accepting certain Id/Class_NAME与不接受某些 Id/Class_NAME 的硒定位器混淆
【发布时间】:2022-09-24 08:45:28
【问题描述】:

我希望有人可以帮助我更好地理解,所以到目前为止我一直引用的网站是https://selenium-python.readthedocs.io/locating-elements.html#locating-elements-by-css-selectors

这是代码(它确实有效)


#https://www.softwaretestinghelp.com/selenium-python-tutorial/#Configuration_Of_Selenium_In_PyCharm
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By


serv = Service(r\"C:\\Users\\BAtMAn\\PycharmProjects\\DellUpdate\\Drivers\\chromedriver.exe\")
driver = webdriver.Chrome(service=serv)
driver.get(\"https://www.dell.com/support\")
driver.maximize_window()



def Step1():
    SearchBar = driver.find_element(By.NAME, \"entry-main-input-home\").send_keys(\"Test\")
    SearchBarClick = driver.find_element(By.ID,\"txtSearchEs\").click()
Step1()


def Popup():
    if driver.find_element(By.ID, \"sec-overlay\"):
        time.sleep(30.5)
        SearchBarClick = driver.find_element(By.ID, \"txtSearchEs\").click()
        time.sleep(5)
Popup()


def FeedBack():
    if driver.find_element(By.CSS_SELECTOR, (\'<button id=\"noButtonIPDell\" class=\"noButton buttons\" aria-label=\"No, thanks\">No, thanks</button>)\'):
        driver.find_element(By.ID, \"button id#noButtonIPDell\").click()
FeedBack()

def Drivers():
    if FeedBack() == False:
        driver.find_element(By.ID, \"drivers\").click()
Drivers()

我的问题是我在找到一个有效的 ID 和 Class_NAME 之前经历了一堆,我想知道是不是我只是不明白我在读什么。下面是我尝试的 class_name 和 ID 的列表,但 driver.find_element 没有找到任何东西。

班级名称, \”\”):自定义 Aka-popup-body 区域 aka-popup-text-center aka-popup-margin-bottom24 自定义-Aka-popup-icon

IDes-alert-notice-duotone 秒覆盖

好的,所以我正在检查的是我不断收到的 30 秒延迟弹出窗口 dell.com/support 弹出窗口似乎只在运行代码时出现,而不是在我手动拉起所有内容时出现。

(最新版本 8/24 3pm gmt-4) 错误(不单击“查找驱动程序下拉菜单”)

selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{\"method\":\"xpath\",\"selector\":\"//iframe[@title=\'Dell民意调查\']\”}


# https://www.softwaretestinghelp.com/selenium-python-tutorial/#Configuration_Of_Selenium_In_PyCharm
\"\"\"
Pip Installs:
Selenium
\"\"\"
import pyautogui
import time
import subprocess
import pyperclip
import re
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By


serv = Service(r\"C:\\Users\\rcowart\\Desktop\\Robert\\Pycharm\\DellUpdate - selenium\\Driver\\chromedriver.exe\")
driver = webdriver.Chrome(service=serv)
driver.get(\"https://www.dell.com/support\")
driver.maximize_window()

def SerialNumber():
    SerialNumber = \'wmic bios get serialnumber\'
    result = subprocess.getoutput(SerialNumber)
    SerialResult = (result.strip(\"SerialNumber\"))
    print(re.sub(\"[^a-zA-Z0-9]+\", \"\", SerialResult))
    pyperclip.copy(re.sub(\"[^a-zA-Z0-9]+\", \"\", SerialResult))
SerialNumber()


def Step1():
    SearchBar = driver.find_element(By.NAME, \"entry-main-input-home\").send_keys(\"3Q84KQ2\")
    SearchBarClick = driver.find_element(By.ID,\"txtSearchEs\").click()
    print(\"Locating SerialNumber\")
Step1()


def Popup():
#30 second popup
    if driver.find_element(By.ID, \"sec-overlay\"):
        time.sleep(30.5)
        driver.find_element(By.ID, \"txtSearchEs\").click()
        print(\"Searching: \")
    else:driver.find_element(By.ID,\"txtSearchEs\").click()
    time.sleep(3)
Popup()

def DriversAndDownloads():
#refresh bypasses the FeedBack not being found
    pyautogui.press(\"f5\")
    if driver.find_element(By.ID, \"drivers\"):
        driver.find_element(By.ID, \"drivers\").click()
        print(\"Successfully Located DriversAndDownloads\")
    else:
        pyautogui.press(\"f5\")
        driver.find_element(By.ID, \"drivers\").click()
        print(\"Refreshing page!\")

def FeedBack():
#popup asking for feedback
    if driver.find_elements(By.ID, \"noButtonIPDell\"):
        driver.find_element(By.ID, \"noButtonIPDell\").click()
        print(\"Sucessfully Located FeedBack: ID\")
    else:
#sleep gives DriversAndDownloads time to refresh/find element without the popup covering it
        print(\"Unable to locate FeedBack!\")
        pyautogui.press(\'f5\')
        time.sleep(3)
        DriversAndDownloads()
FeedBack()


def FindDrivers():
#Drop down
    if driver.find_element(By.XPATH, \"//iframe[@title=\'Dell Survey\']\"):
        driver.find_element(By.XPATH, \"//iframe[@title=\'Dell Survey\']\").click()
        print(\"Successfully located FindDrivers: iframe: Dell Survey\")
    elif  driver.switch_to.frame(\"iframeSurvey\"):
        driver.find_element(By.ID, \"iframeSurvey\").click()
        print(\"Successfully located FindDrivers: iframeSurvey\")
    else:
        print(\"Unable to locate FindDrivers\")
FindDrivers()
  • 仍然在尝试让 ID 进行下一步工作时遇到问题。感谢您提供任何帮助来解决这个问题。
  • 这听起来像X-Y problem。与其寻求解决问题的帮助,不如编辑您的问题并询问实际问题。你想做什么?
  • @undetectedSelenium 我正在尝试检查一些网页上的元素。目前我已经创建了一个函数来检查是否弹出一个 .. 弹出,如果是,请单击否。所以(在 ​​chrome 中)弹出窗口我右键单击 \"no\" 框并检查,它返回<button id=\"noButtonIPDell\" class=\"noButton buttons\" aria-label=\"不,谢谢\">不,谢谢</button>我尝试使用一些选项,但driver.find_element(By.CSS_SELECTOR, (\'<button id=\"noButtonIPDell\" class=\"noButton buttons\" aria-label=\"不,谢谢\">不,谢谢</button>)\' )和 ** driver.find_element(By.ID, \"button id#noButtonIPDell\")*
  • 不知何故,我无法重现不,谢谢按钮。使用手动步骤更新问题。
  • @undetectedSelenium 它要求您使用实际的戴尔序列号(不会每次都弹出,但大部分时间)我将包含(我假设)Html 的图像

标签: python python-3.x selenium classname


【解决方案1】:

如果您仔细观察,“不,谢谢”按钮位于 iframe 内。因此,如果您想对框架内的元素执行任何操作,您首先需要切换到该框架,然后尝试对元素执行所需的操作。

我认为通过 XPath 访问按钮不会有问题。

iframe

【讨论】:

  • 如果这行得通,我会流下喜悦的泪水。我在这几行代码上花了几天时间(所有这些对我来说都是一个新主题“硒,检查元素,autopygui,自动化等”)试图弄清楚如何让它工作
  • 我无法正确获取代码。我找到的文件说我需要先使用一个开关,因为它是一个 iframe。我一直在使用代码..if driver.switch_to.frame("title=Dell.Survey"): print("程序找到了按钮!")和if driver.switch_to.frame("src=about:blank")和 ** 如果 driver.switch_to.frame(id="iframeSurvey")** 和driver.switch_to.frame("//iframe[@src='about:blank']")和driver.switch_to.frame(1)没有运气,您对我可以参考的文档或如何编写代码有任何建议吗?
  • @navi 请尝试以下操作:driver.switch_to.frame("iframeSurvey") 或尝试 driver.switch_to.frame(driver.find_element(By.XPATH, "//iframe[@title='Dell Survey']"))
  • 如果 driver.switch_to.frame("iframeSurvey"):返回 selenium.common.exceptions.NoSuchFrameException:消息:iframeSurvey如果 driver.find_element((By.XPATH, "//iframe[@title='Dell Survey']")): **returned selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'using' must be a细绳 ***
  • 请您尝试使用等待语句好吗?从 selenium.webdriver.support import expected_conditions as EC WebDriverWait(driver, 10).wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'iframeSurvey')))
猜你喜欢
  • 2015-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-20
  • 2012-01-13
  • 1970-01-01
  • 2023-03-29
  • 2019-03-12
相关资源
最近更新 更多