在Python上使用Selenium的CSS选择器找不到任何元素

时间:2021-01-05 19:30:57

I'm running a script on vodafone.co.uk but I can't find any element using the CSS Selector.

我在vodafone.co.uk上运行脚本,但我找不到使用CSS Selector的任何元素。

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()

And this is what I get:

这就是我得到的:

NoSuchElementException: Message: Unable to locate element

Doesn't metter what CSS I use, selenium doesn't find any element anyways. Thank you.

不满足我使用的CSS,selenium无论如何都找不到任何元素。谢谢。

1 个解决方案

#1


0  

As you have not mentioned about which element you want to interact with it.

因为您没有提到要与之交互的元素。

I'm clicking on broadband link on vodafone.co.uk.

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

Code :

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()  

You can write the same thing for your web element.

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

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()

#1


0  

As you have not mentioned about which element you want to interact with it.

因为您没有提到要与之交互的元素。

I'm clicking on broadband link on vodafone.co.uk.

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

Code :

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()  

You can write the same thing for your web element.

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

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()