简体   繁体   中英

Unable to locate element, selenium, python

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 class="_3fiPU _5xe8d " id="UbF6uAnDYp1TNJpC8fVQo" type="text" placeholder="0" data-test="to_amount_input" data-testid="toAmount" autocomplete="off" inputmode="decimal" value="983.65651">

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"]

I solved it by switching to frame. That HTML was in iframe, so i added code below:

driver.get(url)
driver.implicitly_wait(10)
iframe = driver.find_element(by=By.XPATH, value="//iframe[@data-test='main_iframe']")
driver.switch_to.frame(iframe)
val = driver.find_element(by=By.XPATH, value="//input[@data-test='to_amount_input']")
print(val.get_attribute('value'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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