简体   繁体   English

在 selenium python webdriver 中查找元素时出错

[英]Error while finding element in selenium python webdriver

I started with Selenium Webdriver and trying to open google.com and then click on button "Google search".我从 Selenium Webdriver 开始,尝试打开 google.com,然后单击“Google 搜索”按钮。 I got the button element using chropath and below is my code我使用chropath获得了按钮元素,下面是我的代码

from selenium import webdriver
b = webdriver.Chrome()
b.get("http://google.com")
c = b.find_element_by_name("btnk")
c.click()

Getting error as得到错误为

 raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="btnk"]"}
  (Session info: chrome=81.0.4044.138)

Screenshot of chropath chropath 截图

在此处输入图像描述

Edit:编辑:

Below is the code to click on "I am feeling lucky"下面是点击“我感觉很幸运”的代码

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

b = webdriver.Chrome()
b.get("http://google.com")
timeout = 3
try:
    element_present = EC.presence_of_element_located((By.XPATH, "//div[@class='FPdoLc tfB0Bf']//input[@name='btnI']"))
    WebDriverWait(b, timeout).until(element_present)
    c = b.find_elements_by_xpath("//div[@class='FPdoLc tfB0Bf']//input[@name='btnI']")
    print("Value of c ", c)
    for i in c:
        print (i)
    c[0].click()
except TimeoutException:
    print("Timed out")

This is working but why c is a list.这是有效的,但为什么c是一个列表。

I believe the problem is that you are not waiting for the view to be loaded before finding the element.我认为问题在于您在找到元素之前没有等待加载视图。 You can use a Explicit Timeout to wait until btnK is in the view.您可以使用显式超时来等待 btnK 在视图中。

Use the WebDriverWait until presence_of_element_located .使用WebDriverWait直到presence_of_element_located The timeout can be adjusted based on network conditions.超时时间可以根据网络情况进行调整。 It can be seen that in the html, there are two elements with btnK as their name.可以看出,在html中,有两个以btnK为名字的元素。 Although I do not know the reason for that, only one of them is visible, thus c[1] is used to click.虽然我不知道其中的原因,但其中只有一个是可见的,因此c[1]用于单击。

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

b = webdriver.Chrome()
b.get("http://google.com")
timeout = 3
try:
    element_present = EC.presence_of_element_located((By.NAME, 'btnK'))
    WebDriverWait(b, timeout).until(element_present)
    c = b.find_elements_by_name("btnK")
    c[1].click()
except TimeoutException:
    print("Timed out")

If you see DOM you will find two elements for the input button with name btnK and btnI Induce WebDriverWait () and wait for element_to_be_clickable () and following xpath.如果您看到DOM ,您会发现输入按钮的两个元素,名称为btnKbtnI WebDriverWait () 并等待element_to_be_clickable () 和 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

b = webdriver.Chrome()
b.get("http://google.com")
#Close the cookie if there..If not ignore
WebDriverWait(b,5).until(EC.element_to_be_clickable((By.XPATH,"//*[text()='Remind me later']"))).click()

#Click on Google Search
gsearch=WebDriverWait(b,5).until(EC.element_to_be_clickable((By.XPATH,"(//input[@name='btnK'])[last()]")))
gsearch.click()

Imluckuy=WebDriverWait(b,5).until(EC.element_to_be_clickable((By.XPATH,"(//input[@name='btnI'])[last()]")))
Imluckuy.click()

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

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