无法在 python selenium 中使用 selenium chrome webdriver 定位元素 [英] Unable to locate element using selenium chrome webdriver in python selenium

查看:47
本文介绍了无法在 python selenium 中使用 selenium chrome webdriver 定位元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,正在尝试进行一些网页抓取,但遇到了一些实际问题.也许你可以帮我.

I am new to python and trying to do some webscraping but have some real issues. May be you can help me out.

HTML:

<input autocomplete="off" type="search" name="search-search-field" placeholder="150k companies worldwide" data-cy-id="search-search-field" class="sc-dnqmqq grpFhe" value="">

我的代码的第一部分如下所示并且运行良好,没有任何问题:

The first part of my code looks as follows and works good without having any issues:

driver.get("https:")
login = driver.find_element_by_xpath(email_xpath).send_keys(email)
login = driver.find_element_by_xpath(pwd_xpath).send_keys(pwd)
login = driver.find_element_by_xpath(continue_xpath)
login.click()
time.sleep(10)

email 和 pwd 是变量,包括我的登录详细信息.正如我所说,这部分工作得很好.

email and pwd are variables including my login details. As I said that part works pretty fine.

我遇到的问题是以下代码行:

The issues I have are with the following code line:

search = driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/header/div/nav/div[1]/div/div/fieldset/input')

结果我收到以下错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div/div[1]/header/div/nav/div[1]/div/div/fieldset/input"}
 

我试了又试,但无法解决问题.如果有人可以帮助我,我将不胜感激.谢谢!

I tried and tried but could not solve the problem. I would appreciate it very much, if anyone could help me out. Thank you!

推荐答案

要定位搜索字段,您可以使用以下任一定位器策略:

To locate the search field you can use either of the following Locator Strategies:

  • 使用css_selector:

search = driver.find_element_by_css_selector("input[name='search-search-field'][data-cy-id='search-search-field']")

  • 使用xpath:

    search = driver.find_element_by_xpath("//input[@name='search-search-field' and @data-cy-id='search-search-field']")
    

  • 理想情况下,要定位需要诱导的元素 WebDriverWait 用于 element_to_be_clickable() 并且您可以使用以下任一定位器策略:

    Ideally, to locate the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

    • 使用CSS_SELECTOR:

    search = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='search-search-field'][data-cy-id='search-search-field']")))
    

  • 使用XPATH:

    search = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='search-search-field' and @data-cy-id='search-search-field']")))
    

  • 注意:您必须添加以下导入:

  • 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
    

  • 您可以在 上找到一些相关讨论NoSuchElementException 在:

    这篇关于无法在 python selenium 中使用 selenium chrome webdriver 定位元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆