我如何看待硒webdriver的输入? [蟒蛇]

时间:2019-09-24 14:36:16

标签: python selenium selenium-webdriver

我尝试了多种方法来使用硒查找此输入,以便能够测试登录,但是我找不到它,代码在python中,并且尝试了以下操作:

大多数评论是我所做的尝试。

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
from selenium.webdriver.common.keys import Keys
import selenium.webdriver.support.ui as ui



from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time


browser = webdriver.Firefox()
wait = ui.WebDriverWait(browser, 10)

browser.get("https://www.losango.com.br/crednetv2/htm/index.htm") 

time.sleep(5)

# //*[@id="txtLoja"]
# /html/body/form/center/table[2]/tbody/tr[1]/td[2]/table/tbody/tr[3]/td/input[@id="txtLoja"]
# browser.find_element_by_css_selector(".input[id='txtLoja']").click()
contador = 0
while contador < 3:
    try:

        loja = browser.find_element_by_tag_id('txtLoja')#atrasves do id
        loja.clear()
        # loja = browser.find_element_by_id('txtLoja')#por esse jeito também
        # loja = browser.find_element_by_name('txtLoja')#e pelo name
        # loja = browser.find_elements(By.XPATH, '//*[@id="txtLoja"]')#ataves do xpath
        print("achou essa loja")
        loja.send_keys('numeroLoja')

    except:
        print('Falha.')
        time.sleep(5)
        contador = contador + 1
    else:
        break

1 个答案:

答案 0 :(得分:3)

输入框位于iframe

您需要先切换到iframe才能访问元素。

得出WebDriverWaitframe_to_be_available_and_switch_to_it()

得出WebDriverWaitelement_to_be_clickable()

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

driver=webdriver.Firefox()
driver.get("https://www.losango.com.br/crednetv2/htm/index.htm")
WebDriverWait(driver,20).until(ec.frame_to_be_available_and_switch_to_it((By.NAME,"main")))
WebDriverWait(driver,20).until(ec.element_to_be_clickable((By.NAME,"txtLoja"))).send_keys("matheus")

浏览器快照:

enter image description here