你好,我是新来的,我有一个问题.它是关于我的网站的帐户创建者。..
email = driver.find_element_by_xpath('//*[@id="elInput_email"]')
email.send_keys(email_str)
有时显示错误。消息。Unable to locate element: /*[@id="elInput_email"]。
关闭浏览器而不检查 我怎么做最好?
我尝试
if not driver.find_element_by_xpath('//*[@id="elInput_email"]'):driver.close()
else: email.send_keys(email_str)
但不工作。
我希望webdrive(浏览器)如果不能定位XPATH,就会被关闭。
这里是伪代码。
需要导入Explcit wait
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
脚本应该是
# initiate your driver instance
# navigate to the page
try:
# wait for the element using explicit wait(waiting for max of 10 seconds)
wait = WebDriverWait(driver, 10)
email = wait.until(EC.presence_of_element_located((By.XPATH,'//*[@id="elInput_email"]')))
email.send_keys(email_str)
finally:
# close the browser here
driver.quit()