如何使用Selenium发送多个标签?
当我跑:
uname = browser.find_element_by_name("text")
uname.send_keys(Keys.TAB)
Run Code Online (Sandbox Code Playgroud)
选择下一个元素.当uname.send_keys(Keys.TAB)再次执行时没有任何反应 - 实际上选择了下一个元素uname- >所以它与运行一次时相同.
我怎样才能多次前进 - 基本上我会多次手动按TAB?
这是没有错误的代码:
perform() 和 reset_actions()
但这两个功能必须结合使用
import os
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import random
# Setting the chrome_options
global chrome_options
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument('--profile-directory=Default')
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.add_argument('disable-infobars')
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
google_search = [
"1.' driver.switch_to.active_element' ",
"2.this code is a one of important snippet for facebook automation.",
]
random_google_search = random.choice(google_search)
# Setting the Chrome Driver
global driver
driver = webdriver.Chrome("chromedriver.exe", …Run Code Online (Sandbox Code Playgroud) 总而言之,我需要一些严肃的"愚蠢"的例子,说明如何在Python中编写代码,如果元素存在或不存在则会执行某些操作.我是编程的新手,我已经回顾了几天的帖子,我似乎无法弄清楚....
这是我想要做的.
from selenium.common.exceptions import NoSuchElementException, staleElementReferenceException
elem = driver.find_element_by_partial_link_text('Create Activity')
print("Searching for Create Activity.")
if elem.is_displayed():
elem.click() # this will click the element if it is there
print("FOUND THE LINK CREATE ACTIVITY! and Clicked it!")
else:
print ("NO LINK FOUND")
Run Code Online (Sandbox Code Playgroud)
所以,假设存在一个LINK,即element_by_partial_link_text('创建活动')
我得到了正确的答复......正在搜索创建活动.发现链接创造活动!并点击它!
我的问题是没有匹配的链接 element_by_partial_link_text('Create Activity')
我没有得到我在其他声明中所期望的东西.
print ("NO LINK FOUND")
我明白了......
回溯(最近一次调用最后一次):文件"",第1行,文件"C:\ Program Files(x86)\ Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py",第341行,在find_element_by_partial_link_text中返回self.find_element(by = By.PARTIAL_LINK_TEXT,value = link_text)文件"C:\ Program Files(x86)\ Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py",第745行,在find_element {'using':by,'value':value})['value']文件"C:\ Program Files(x86)\ Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py",第236行,执行self.error_handler.check_response(响应)文件"C:\ Program Files(x86)\ Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py" ,第194行,在check_response中引发exception_class(message,screen,stacktrace)selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{"method":"部分链接文本","selector":"创建活动"}
我如何获得selenium Python来转换此异常或错误以不停止我的脚本并只处理ELSE语句.
谢谢,