How to Find Element by Part of Its Id Name in Selenium with Python

How to find element by part of its id name in selenium with python

To find the element which you have located with:

sixth_item = driver.find_element_by_id("coption5")

To locate this element only by using coption you can use can use either of the following Locator Strategies:

  • Using XPATH and starts-with():

    sixth_item = driver.find_element_by_xpath("//*[starts-with(@id, 'coption')]")
  • Using XPATH and contains():

    sixth_item = driver.find_element_by_xpath("//*[contains(@id, 'coption')]")
  • Using CSS_SELECTOR and ^ (wildcard of starts-with):

    sixth_item = driver.find_element_by_css_selector("[id^='coption']")
  • Using CSS_SELECTOR and * (wildcard of contains):

    sixth_item = driver.find_element_by_css_selector("[id*='coption']")


Reference

You can find a detailed discussion on dynamic CssSelectors in:

  • How to get selectors with dynamic part inside using Selenium with Python?
  • Java Selenium webdriver expression finding dynamic element by ccs that starts with and ends with
  • How to click a dynamic link with in a drupal 8 website using xpath/css selector while automating through Selenium and Python
  • Finding elements by CSS selector with ChromeDriver (Selenium) in Python

How to find a web element based upon it's tag name and attributes using Selenium and Python

You saw it right. The Phone number, username, or email <input> field has the name set as username.

<input aria-label="Phone number, username, or email" aria-required="true" autocapitalize="off" autocorrect="off" maxlength="75" name="username" type="text" class="_2hvTZ pexuQ zyHYP" value="">

So to locate the element you can use either of the following Locator Strategies:

  • Using css_selector:

    element = driver.find_element(By.CSS_SELECTOR, "input[name='username']")
  • Using xpath:

    element = driver.find_element(By.XPATH, "//input[@name='username']")

As the element is an interactive element 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:

  • Using CSS_SELECTOR:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']")))
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='username']")))
  • 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

How to get an element by tag name or id in Python and Selenium

Use this when you want to locate an element by class name. With this strategy, the first element with the matching class name attribute will be returned. If no element has a matching class name attribute, a NoSuchElementException will be raised.

For instance, consider this page source:

<html>
<body>
<p class="content">Site content goes here.</p>
</body>
</html>

The “p” element can be located like this:

content = driver.find_element_by_class_name('content')

https://selenium-python.readthedocs.io/locating-elements.html

Is it possible to locate element by partial id match in Selenium

You can apply an ends-with CSS selector:

By.cssSelector("[id$=default-create-firstname]")

Update

Some time went by since the answer was posted. Here some update from the linked mozilla developer page and comments below:

New use By.css instead of By.cssSelector

By.css("[id$=default-create-firstname]")

Also see the four possibilities of

  • beginning with
  • anywhere inside
  • anywhere inside regardless capitalization
  • end with

/* Internal links, beginning with "#" */
a[href^="#"] {
background-color: gold;
}

/* Links with "example" anywhere in the URL */
a[href*="example"] {
background-color: silver;
}

/* Links with "insensitive" anywhere in the URL,
regardless of capitalization */
a[href*="insensitive" i] {
color: cyan;
}

/* Links that end in ".org" */
a[href$=".org"] {
color: red;
}

Choose element by partial ID using Selenium with python?

Although the answer is in C#, the underlying solution is still the same, by using CSS selectors:

driver.find_elements_by_css_selector('input[id*='cell.line.order(240686080)']')

or XPath will also be able to do this:

driver.find_elements_by_xpath('//*[contains(@id, 'cell.line.order(240686080)')]')

Cannot locate a partial id on selenium

To follow the @murali's point about the a versus input, you can just fix your CSS selectors:

usr = driver.find_element_by_css_selector("input[id*=txtEmail]")
pwd = driver.find_element_by_css_selector("input[id*=txtPassword]")

You can also use the "ends-with" check instead of "contains":

usr = driver.find_element_by_css_selector("input[id$=txtEmail]")
pwd = driver.find_element_by_css_selector("input[id$=txtPassword]")

How to correctly find elements by class name on Python? Selenium related

You are using this CLASS_NAME elements__StyledListItem-sc-197zmwo-0 QbTKh which has space in it.

In Selenium, a class name having space will not be parsed and will throw the error.

The reason why you did not get the error is cause you are using find_elements that will either return a list of web element or nothing.

So how to resolve this?

remove the space and put a . instead to make a CSS_SELECTOR

try this:

wallet_providers = driver.find_elements(By.CSS_SELECTOR, ".elements__StyledListItem-sc-197zmwo-0.QbTKh") #get the list of wallet providers

to be honest we can have better locator than this, cause it seems these values 197zmwo-0.QbTKh are generated dynamically.

I would rather use this CSS:

li[class^='elements__StyledListItem'] span[font-weight]

or this xpath:

//li[starts-with(@class,'elements__StyledListItem')]//descendant::span[@font-weight]

Also, you should print it like this: (this is one way but there are others as well):

Code:

driver.get("https://opensea.io/")

WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="__next"]/div/div[1]/nav/ul/div[2]/li/button'))) #wait for the wallet button to be enabled for clicking
wallet_button = driver.find_element(By.XPATH, '//*[@id="__next"]/div/div[1]/nav/ul/div[2]/li/button')
wallet_button.click() #click that wallet button
wallet_providers = driver.find_elements(By.CSS_SELECTOR, "li[class^='elements__StyledListItem'] span[font-weight]") #get the list of wallet providers
for i in wallet_providers:
print(i.get_attribute('innerText'))

Console output:

WalletConnect
MetaMask
Coinbase Wallet
Fortmatic

Process finished with exit code 0


Related Topics



Leave a reply



Submit