本文介绍了硒(Python)find_element_by_id在schwab.com上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Selenium(通过Python),我试图找到 http://schwab.com 的登录"按钮一个>.该按钮是BUTTON类型的元素,且id ='loginSubmitButton'.我正在使用以下代码:

Using Selenium (via Python), I am trying to locate the "Login" button of http://schwab.com.The button is an element of type BUTTON and id='loginSubmitButton'.I am using the following code:

from selenium import webdriver

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://schwab.com")
driver.implicitly_wait(10)
driver.find_element_by_id("loginSubmitButton")
driver.close()

浏览器正确地打开了页面,并且按钮确实位于此处(使用Chrome开发工具),但是Selenium无法找到它.

The browser correctly opens the page and the button is verifiably there (using Chrome dev tools), however Selenium fails to locate it.

我已经尝试了此代码的许多变体,包括使用WebDriverWait,但似乎无济于事.

I have tried many variations of this code, including using WebDriverWait, but nothing seems to work.

建议表示赞赏.

推荐答案

由于所需元素位于<iframe>之内,因此必须在该元素上调用click():

As the the desired element is within an <iframe> so to invoke click() on the element you have to:

  • 诱导 WebDriverWait 以使所需的框架可用并切换到.
  • 诱导 WebDriverWait 以使所需的元素可点击.
  • 您可以使用以下定位器策略:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be clickable.
  • You can use either of the following Locator Strategies:

  • 使用CSS_SELECTOR:

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

driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get('https://www.schwab.com/')
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#LoginComponentForm")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span#LoginText"))).click()

  • 使用XPATH:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
    driver.get('https://www.schwab.com/')
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='LoginComponentForm']")))
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[@id='LoginText']"))).click()
    

  • 此处您可以在在下面的方法中处理#document的方法找到相关的讨论iframe

    这篇关于硒(Python)find_element_by_id在schwab.com上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    07-31 01:54