Home > Blockchain >  python selenium, i can't find element class or id from a text box
python selenium, i can't find element class or id from a text box

Time:12-21

i need some help here, i try to fill a text box but when i inspect the element then copy the xpath it's only give me

/body/html

then, i try to use the class name, but it's doesn not work

how i can solve this?

here is my code :

from selenium import webdriver
driver = webdriver.Chrome()
driver.get('link_to_the_website')

driver.find_element_by_xpath('/body/html').send_keys('hello world')

textboxes = driver.find_element_by_xpath('/html/body')

here is the html code when i inspect the text box

<body marginwidth="0" marginheight="0"  spellcheck="true" style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); cursor: text; font-family: &quot;Open Sans&quot;, sans-serif; font-size: 11px; font-style: normal; font-variant: normal; font-weight: 400; line-height: 16.5px; letter-spacing: normal; text-align: start; text-decoration: none solid rgb(0, 0, 0); text-indent: 0px; text-rendering: auto; word-break: normal; overflow-wrap: break-word; word-spacing: 0px;" contenteditable="true">type here......</body>

the link :

http://simasunkinerja.com/public/Transaksilhe/create?id=Mg==&act=rekomendasi

we need to login : user = IRB00001 password = IRB00001

in that link we'll see several the text boxes

CodePudding user response:

When you right click and inspect element, try to look if it has a name first and use driver.find_element_by_name("") accordingly.

if no name is available, the next stop should be to find the element by css-selector driver.find_element_by_css_selector("").

Xpath is usually the last thing you'd like to use. Incidentally, the link you provided does have a xpath when I copy it. It would be /html/body/div[3]/div[1]/div[2]/div/form/div[1]/div/input[1]

CodePudding user response:

  1. The element you are trying to access is inside iframe.
  2. You should use explicit waits
  3. Your locator is wrong...
    This may work better:
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.Chrome()
wait = WebDriverWait(driver, 20)
driver.get('link_to_the_website')
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"(//iframe)[1]")))
textarea = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "body.textarea")))
textarea.send_keys(your_text)

CodePudding user response:

wait=WebDriverWait(driver,10)
wait.until(EC.element_to_be_clickable((By.NAME,"email"))).send_keys("IRB00001")
wait.until(EC.element_to_be_clickable((By.NAME,"password"))).send_keys("IRB00001")

Just wait and grab by name email and password

Import:

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