Home > Mobile >  Selenium cant find element by id, yet it exists
Selenium cant find element by id, yet it exists

Time:10-21

So basically my script goes to the product-creation page of my shop, then logs in, after redirect it should put in the product title.

For that i wanted to use selenium, as this shop system has no useful API features for me.

the code that does the trick is following:

from selenium import webdriver
import time

browser = webdriver.Firefox()

url3 = 'https://mywebsite.net/admin#/sw/product/create/base'
browser.get(url3)
swu = 'admin'
swp = 'password'
browser.find_element_by_id('sw-field--username').send_keys(swu)
browser.find_element_by_id(
    'sw-field--password').send_keys(swp)
browser.find_element_by_class_name('sw-button__content').click()
time.sleep(5)
browser.find_element_by_id(
    'sw-field--product-name').send_keys('dsdsdsdssds')


However, my script perfectly recognizes the admin and password field by id, but after login and redirect it cant recognize the product title field.

The shop system is shopware 6

CodePudding user response:

As you haven't provided any HTML to work with (or a reprex), I can't give an answer specific to your use case. However, the solution is likely to be that you need to use Selenium's expected conditions:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("https://mywebsite.net/admin#/sw/product/create/base")
...
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    driver.quit()

Another notable cause of the element not being visible to selenium is when it's contained within an iframe. If that's the case, you'll need to use switch_to_frame as described in the documentation.

  • Related