【问题标题】:Can't find any element using CSS selector with Selenium on Python在 Python 上使用带有 Selenium 的 CSS 选择器找不到任何元素
【发布时间】:2018-06-16 23:28:55
【问题描述】:

我正在 vodafone.co.uk 上运行一个脚本,但我无法使用 CSS 选择器找到任何元素。

browser.get("http://freesim.vodafone.co.uk/")
 browser.find_element_by_css_selector("#frmNew > div:nth-child(32) > div > div > div > div.freesim-text-last.last > button > span").click()

这就是我得到的:

NoSuchElementException: Message: Unable to locate element

不知道我使用什么 CSS,selenium 无论如何都找不到任何元素。谢谢。

【问题讨论】:

  • 你能告诉我们这代表哪个元素“#frmNew > div:nth-child(32) > div > div > div > div.freesim-text-last.last > button > span” ?
  • 哇,这一定是我见过的最复杂的 css celector。是的,请告诉我们您想点击什么,我们可以提供帮助

标签: python css selenium


【解决方案1】:

正如您没有提到要与哪个元素进行交互一样。

我正在点击vodafone.co.uk 上的宽带链接。

代码:

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

driver = webdriver.Chrome(executable_path = r'D:/Automation/chromedriver.exe')
driver.get("http://freesim.vodafone.co.uk/")

broad_band = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='https://www.vodafone.co.uk/broadband/index.htm'] button"))) 

broad_band.click()  

您可以为您的网络元素编写相同的内容。

button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#frmNew > div:nth-child(32) > div > div > div > div.freesim-text-last.last > button > span")))  
button.click()

【讨论】:

  • 抱歉回复晚了。我设法通过在更改框架后找到元素来解决这个问题
  • @Beter:好的,这个答案有帮助吗?
  • @Beter:谢谢。祝你有美好的一天!
猜你喜欢
  • 2013-07-29
  • 2014-08-08
  • 2023-03-16
  • 2022-01-07
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 2018-03-28
  • 2018-10-22
相关资源
最近更新 更多