下面我为连接的用户名和密码的HTML代码:
用户名:
<input class="form-control input-sm snl-widgets-input-text snl-selectable action" data-part="input" data-bind="snlEnable: enable,value: value,valueUpdate: valueUpdate,visible: visible,css: { 'tags': enableTagsInput, 'clear': enableClear, 'search': enableSearch, 'action': externalActionVisible, 'pseudoTags': enablePseudoTags },hasFocus: hasFocus,attr: { type: type, name: name, placeholder: placeholder, maxlength: maxlength }" type="text" name="username" placeholder="Email address" maxlength="524288">
密码:
<input class="form-control input-sm snl-widgets-input-text snl-selectable action" data-part="input" data-bind="snlEnable: enable,value: value,valueUpdate: valueUpdate,visible: visible,css: { 'tags': enableTagsInput, 'clear': enableClear, 'search': enableSearch, 'action': externalActionVisible, 'pseudoTags': enablePseudoTags },hasFocus: hasFocus,attr: { type: type, name: name, placeholder: placeholder, maxlength: maxlength }" type="password" name="password" placeholder="Password" maxlength="524288">
我使用Python 3.7。
我使用了提供权当点击和要求的XPath以下的XPath时,有一个“无法位于元素”的错误:
username = driver.find_element_by_xpath("//*[@id=applicationHost]/div/div[2]/div[2]/div[4]/div[11]/div/div[2]/div[1]/div/form/div/div/div/div[3]/div[2]/div/div[1]/div[1]/div[1]/input")
password = driver.find_element_by_xpath("//*[@id=applicationHost]/div/div[2]/div[2]/div[4]/div[11]/div/div[2]/div[1]/div/form/div/div/div/div[3]/div[2]/div/div[1]/div[1]/div[2]/input")
我也建议明确等待的元素。
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()
url="http://www.example.com"#your url here
driver.get(url)
element=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH ,'//img[starts-with(@id,'removeImage_')]')))
#or '//img[contains(@id,'removeImage_')]'
element.click()
随着您发布的片断,我可以建议你:
from selenium.webdriver.common.by import By
driver.find_element(By.ID, "removeImage_E61858CD-F1F9-42BD-8848-8CB6B42ED2FE")
另一种方式可能是与XPATH。一个可以用来检索XPATH的许多方面,是在浏览器中(即火狐)打开它,右键单击元素,检查(Q),复制,XPath和您必须使用XPath:
driver.find_element(By.XPATH, 'your[4]/xPath/to[2]/element')
如果你想点击元素中,添加点击()结尾:
driver.find_element(By.ID, "removeImage_E61858CD-F1F9-42BD-8848-8CB6B42ED2FE").click().
我不建议你一个很好的教程,但在这里你可以找到一个documentation
你可以找到名字的元素...
username = driver.find_element_by_name('username')
password = driver.find_element_by_name('password')