Я хочу проверить элемент и проверить, существует ли он или нет. Я пробовал много кодов, и все я получил ошибки. Я пробовал это с "если driver.find ...", но я получил ошибки. Есть ли способ проверить элемент без ошибок? Я использовал селен.
Я пробовал это:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
driver = webdriver.Firefox()
driver.get("https://www.instagram.com/accounts/login/")
def check_exists_by_xpath(xpath):
try:
webdriver.find_element_by_xpath("//input[@name='username']")
print ("Alert")
raw_input("")
except NoSuchElementException:
print ("Nothing")
raw_input("")
И у меня есть это:
>>> from selenium import webdriver
>>> from selenium.webdriver.common.keys import Keys
>>> from selenium.common.exceptions import NoSuchElementException
>>>
>>> driver = webdriver.Firefox()
>>> driver.get("https://www.instagram.com/accounts/login/")
>>>
>>> def check_exists_by_xpath(xpath):
... try:
... webdriver.find_element_by_xpath("//input[@name='username']")
... print ("Alert")
File "<stdin>", line 4
print ("Alert")
^
IndentationError: unexpected unindent
>>> raw_input("")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'raw_input' is not defined
>>> except NoSuchElementException:
File "<stdin>", line 1
except NoSuchElementException:
^
IndentationError: unexpected indent
>>> print ("Nothing")
File "<stdin>", line 1
print ("Nothing")
^
IndentationError: unexpected indent
>>> raw_input("")