[英]Writing a string to a spesific field with find_element selenium
Hello I want to write my e-mail automatically to a field on a website.您好,我想将我的电子邮件自动写入网站上的某个字段。 The code that I'm trying is
我正在尝试的代码是
driver.find_element_by_variable("variable").send_keys(username)
Normally I can do what I want on different websites but for this website I guess HTML codes are written little bit different.通常我可以在不同的网站上做我想做的事,但对于这个网站,我猜 HTML 代码写得有点不同。 Here is the HTML code.
这是 HTML 代码。
<div class="mat-form-field-infix ng-tns-c58-0"><input placeholder="jane.doe@email.com" matinput=""
<input placeholder="jane.doe@email.com" matinput="" formcontrolname="username" type="text"
autocomplete="off"
class="mat-input-element mat-form-field-autofill-control ng-tns-c58-0 ng-pristine ng-invalid cdk-text-field-autofill-monitored ng-touched"
id="mat-input-0" data-placeholder="jane.doe@email.com" aria-invalid="false" aria-required="false">
How should I write the find_element
code?我应该如何编写
find_element
代码?
I can't be sure about the correct locator for that element since you didn't share a link to that page, however you can try the following:由于您没有共享指向该页面的链接,因此我无法确定该元素的正确定位器,但是您可以尝试以下操作:
There are several possible locators that may work for this element.有几个可能的定位器可以用于这个元素。 You can try this:
你可以试试这个:
input = driver.find_element_xpath("//input[@formcontrolname='username']")
input.click()
input.send_keys(username)
driver.find_element_by_variable() isn't a valid Locator Strategy driver.find_element_by_variable()不是有效的定位器策略
To send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :要将字符序列发送到您需要为element_to_be_clickable()诱导WebDriverWait的元素,您可以使用以下任一Locator Strategies :
Using CSS_SELECTOR
:使用
CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.mat-input-element[id^='mat-input'][placeholder='jane.doe@email.com'][data-placeholder='jane.doe@email.com']"))).send_keys("text")
Using XPATH
:使用
XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[contains(@class, 'mat-input-element') and starts-with(@id, 'mat-input')][@placeholder='jane.doe@email.com' and @data-placeholder='jane.doe@email.com']"))).send_keys("berkay_doruk")
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.