简体   繁体   English

无法找到 Xpath 元素

[英]Unable to locate Xpath Element

Hi I am fairly new to selenium .Can somebody please suggest on how to locate and element inside iframe asi am getting error below.嗨,我对硒相当陌生。有人可以建议如何在 iframe 中定位和元素,下面出现错误。

I am trying to implement an Automation Script on Salesforce Pardot page and there are 2 iframes and i want to access Button on 1st iframe Tag and click on same.我正在尝试在 Salesforce Pardot 页面上实现自动化脚本,并且有 2 个 iframe,我想访问第一个 iframe 标签上的按钮并单击相同的按钮。

Error:- selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[starts-with(@class,'slds-button_reset')]"} (Session info: chrome=80.0.3987.122)错误:- selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{"method":"xpath","selector":"///*[starts-with(@class,'slds- button_reset')]"}(会话信息:chrome=80.0.3987.122)

Screenshot for DOM Button Element DOM 按钮元素的屏幕截图

Screenshot for DOM Button Element along with Iframe tag DOM 按钮元素和 Iframe 标签的屏幕截图

Code Written Previously以前写的代码

from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory': r'C:\Pardot'}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(executable_path="D:\XXX XXXX\XXXX\drivers\chromedriver.exe", options=chrome_options)
driver.get('https://pi.pardot.com/engagementStudio/studio#/15627/reporting')
user_name = driver.find_element_by_css_selector('#email_address')
user_name.send_keys('XXXXXXXXXXXXXXXXXXX')
password = driver.find_element_by_css_selector('#password')
password.send_keys('XXXXXXXXXXXXXXXXX)
submit_button = driver.find_element_by_css_selector('input.btn')
submit_button.click()
iframe_list =  driver.find_elements_by_tag_name("iframe")
driver.switch_to.frame(iframe_list[0])
driver.find_element_by_xpath("//*[starts-with(@class,'slds-button_reset')]")
driver.close()

As the the desired element is within an <iframe> so to invoke click() on the element you have to:由于所需元素位于<iframe>因此要在元素上调用click() ,您必须:

  • Induce WebDriverWait for the desired frame_to_be_available_and_switch_to_it() . Induce WebDriverWait以获得所需的frame_to_be_available_and_switch_to_it()
  • Induce WebDriverWait for the desired element_to_be_clickable() .为所需的element_to_be_clickable()引入WebDriverWait
  • You can use either of the following Locator Strategies :您可以使用以下任一定位器策略

    • Using CSS_SELECTOR :使用CSS_SELECTOR

       WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#content-frame"))) WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-qa='reporting-filter-trigger-toggle'][data-ember-action]"))).click()
    • Using XPATH :使用XPATH

       WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='content-frame']"))) WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@data-qa='reporting-filter-trigger-toggle' and @data-ember-action]"))).click()
  • Note : You have to add the following imports :注意:您必须添加以下导入:

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

Reference参考

You can find a couple of relevant discussions in:您可以在以下位置找到一些相关讨论:

试试这个://li[@id='ember943']

You need to get iframe tag first and switch driver to it like following.您需要先获取 iframe 标记并将驱动程序切换到它,如下所示。

driver.switch_to.frame(driver.find_element_by_tag_name("iframe")) assuming that driver is a healthy instance of webdriver. driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))假设驱动程序是一个健康的 webdriver 实例。 To continue with the default content do driver.switch_to.default_content()要继续使用默认内容,请执行driver.switch_to.default_content()

Please refer this link请参考这个链接

I hope that you're switching to the correct Iframe to fetch the element... Please see the example below我希望你切换到正确的 Iframe 来获取元素......请看下面的例子

driver.switchTo().frame("content-frame")
driver.findElement(By.xpath("//div[@id='ember740']/div[2]/div/ol/li/div/button")
driver.switchTo().defaultContent()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM