简体   繁体   English

如何在 Python Selenium 中找到这个元素

[英]How do I get to this element in Python Selenium

I'm trying to select this element with Python Selenium, but don't know how since it does not contain and id/class name/etc..我正在尝试 select 这个元素与 Python Selenium,但不知道如何,因为它不包含和 id/类名/等。

Here is the HTML:这是 HTML:

<img src="/resource/1599091587000/nforce__SLDS0102/assets/icons/utility/chevrondown_60.png">

I've tried:我试过了:

chrome_browser.find_element_by_xpath("//img[contains(text(),'/resource/1599091587000/nforce__SLDS0102/assets/icons/utility/chevrondown_60.png')]")

Since text() searches for the inner text for the HTML tag, you should not use that to search for this particular element since the URL is in the src attribute.由于text()搜索 HTML 标记的内部文本,因此您不应使用它来搜索此特定元素,因为 URL 位于src属性中。

You may search it using the src attribute您可以使用src属性搜索它

//img[@src='/resource/1599091587000/nforce__SLDS0102/assets/icons/utility/chevrondown_60.png')]

or just a part of it using或者只是其中的一部分使用

//img[contains(@src,'chevrondown_60')]

The element seems to be a dynamic element.该元素似乎是一个动态元素。 So to locate the element you can use either of the following Locator Strategies :因此,要定位元素,您可以使用以下任一Locator Strategies

  • Using css_selector :使用css_selector

     element = chrome_browser.find_element_by_css_selector("img[src^='/resource'][src$='assets/icons/utility/chevrondown_60.png']")
  • Using xpath :使用xpath

     element = chrome_browser.find_element_by_xpath("//img[starts-with(@src, '/resource') and contains(@src, 'assets/icons/utility/chevrondown_60.png')]")

Ideally, to locate the element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies :理想情况下,要定位您需要为visibility_of_element_located()诱导WebDriverWait的元素,您可以使用以下任一Locator Strategies

  • Using CSS_SELECTOR :使用CSS_SELECTOR

     element = WebDriverWait(chrome_browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "img[src^='/resource'][src$='assets/icons/utility/chevrondown_60.png']")))
  • Using XPATH :使用XPATH

     element = WebDriverWait(chrome_browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//img[starts-with(@src, '/resource') and contains(@src, 'assets/icons/utility/chevrondown_60.png')]")))
  • 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

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

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