Home > Enterprise >  Unable to locate element, selenium, python
Unable to locate element, selenium, python

Time:07-06

I tried to get usdt value from this url: https://exchange.mercuryo.io/?currency=USDT&fiat_amount=1000&fiat_currency=EUR&merchant_transaction_id=687fed73-2ecf-e5a5-d53d-bc6555cf92f2&theme=trustwallet&utm_medium=referral&utm_source=TrustWallet&widget_id=d13d7a03-f965-4688-b35a-9d208819ff4b&address=0x6AEa3bAD71F023515032eAcf343119e27f03Af4F

but got an error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[contains(@class,'_5xe8d')]"}

my code:

driver.get(url_eur)
sleep(7)
eur = driver.find_element(by=By.XPATH , value="//input[contains(@class,'_5xe8d')]")
print(eur)

and HTML:

<input  id="UbF6uAnDYp1TNJpC8fVQo" type="text" placeholder="0" data-test="to_amount_input" data-testid="toAmount" autocomplete="off" inputmode="decimal" value="983.65651">

CodePudding user response:

Take a look to this post, which may help you : https://stackoverflow.com/a/48472940/17595642

There are the main Reasons of your Error

The reason for NoSuchElementException can be either of the following :

  • The Locator Strategy you have adopted doesn't identifies any element in the HTML DOM.
  • The Locator Strategy you have adopted is unable to identify the element as it is not within the browser's Viewport.
  • The Locator Strategy you have adopted identifies the element but is invisible due to presence of the attribute style="display: none;".
  • The Locator Strategy you have adopted doesn't uniquely identifies the desired element in the HTML DOM and currently finds some other hidden / invisible element. The WebElement you are trying to locate is within an tag.
  • The WebDriver instance is looking out for the WebElement even before the element is present/visibile within the HTML DOM.

Try out one of these solution if your code is working and you are sure that the element is supposed to be found with this class.

To wait for presenceOfElementLocated :

new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));

To wait for visibilityOfElementLocated :

new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));

To wait for elementToBeClickable :

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));

CodePudding user response:

Your solution - follow "data-*" attributes

Do not try to find DOM elements by potentially mutable class values (example - React element can have random class values on each frontend redeploy)

Try to locate following

//input[@data-testId="toAmount"]

  • Related