So I tried to find this input by many ways and it simple can't be found
<div style="min-height:26px">
<div style="display: inherit;vertical-align:top;">
<input widget="tentry" type="text" required="" aria-required="true" name="nome" value="" id="tentry_1800501884">
::after
</div>
</div>
Here some of what I tried:
By class: "col-sm-8 col-lg-10 fb-field-container"
By class: "fb-inline-field-container form-line"
By class: "form-control tfield"
By ID: "tentry_1381418948"
By css selector: "Input[class*='form-control tfield'" and with the 2 other classes
By xpath: "'//div[contains(@class, "form-control tfield")]'" and with the 2 other classes
Most of the errors i got are: 'str' object is not callable
Does anyone have an idea what i can do?
Here is my code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from pytube import YouTube
from pytube import Channel
import time
import pyautogui
def subir_videos(login, senha):
options = Options()
options.add_argument("start-maximized")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get('http://simplesprefeitura.pompeu.mg.gov.br/index.php?class=WelcomeView')
time.sleep(1)
botao_login = driver.find_element(By.XPATH, ("//span[.='Login']"))
botao_login.click()
time.sleep(1)
botao_login_2 = driver.find_elements(By.XPATH, ("//span[.='Login']"))
botao_login_2[1].click()
time.sleep(1)
campo_usuario = driver.find_element(By.XPATH, ('//div[contains(@class, "fb-inline-field-container form-line")]'))
# campo_usuario = driver.find_element(By.XPATH, ('//div[contains(@class, "col-sm-12 display-flex fb-field-container")]'))
campo_usuario.click()
time.sleep(1)
pyautogui.write(login)
time.sleep(1)
pyautogui.hotkey('tab')
time.sleep(1)
pyautogui.write(senha)
time.sleep(1)
pyautogui.hotkey('enter')
time.sleep(3)
botao_pesquisar = driver.find_element(By.CLASS_NAME, "js-search")
botao_pesquisar.click()
time.sleep(1)
pyautogui.write('videostvlist')
time.sleep(1)
pyautogui.hotkey('enter')
time.sleep(1)
botao_novo = driver.find_element(By.XPATH, "//*[contains(text(), 'Novo')]")
botao_novo.click()
time.sleep(1)
And here is where im getting trouble
campo_nome = driver.find_element(By.CSS_SELECTOR("Input[class*='fb-inline-field-container form-line'"))
campo_nome.click()
time.sleep(1)
UPDATE: i found that if i hit tab 37 times i get there so i'm using it like this while it works
CodePudding user response:
Double check that is not inside a shadow dom or iframe, you may need to switch to it
CodePudding user response:
To identify the input
element use either following css selector
or xpath
css selector:
input.form-control.tfield[name='nome'][id^='tentry_']
or
input.form-control.tfield[id^='tentry_']
xpath:
//input[@class='form-control tfield'][@name='nome'][starts-with(@id, 'tentry_')]
or
//input[@class='form-control tfield'][starts-with(@id, 'tentry_')]
Ideally your code will be
driver.find_element(By.CSS_SELECTOR, "input.form-control.tfield[name='nome'][id^='tentry_']")
Or
driver.find_element(By.XPATH, "//input[@class='form-control tfield'][@name='nome'][starts-with(@id, 'tentry_')]")
UPDATE:
It seems its overlapping the element to click on that element induce javascripts executor.
driver.execute_script("arguments[0].click();", driver.find_element(By.CSS_SELECTOR, "input.form-control.tfield[name='nome'][id^='tentry_']"))
CodePudding user response:
To identify the element you can use either of the following locator strategies:
Using css_selector:
element = driver.find_element(By.CSS_SELECTOR, "input.form-control.tfield[id^='tentry'][widget='tentry'][name='nome']")
Using xpath:
element = driver.find_element(By.XPATH, "//input[@class='form-control tfield' and starts-with(@id, 'tentry')][@widget='tentry' and @name='nome']")
To click on 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:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control.tfield[id^='tentry'][widget='tentry'][name='nome']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='form-control tfield' and starts-with(@id, 'tentry')][@widget='tentry' and @name='nome']"))).click()
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