Selenium发送密钥不起作用



发送键在Mac OS X上不起作用,或者我做错了什么。也许我是指钥匙错误。

我正在尝试单击每个链接以在新标签中打开

有什么建议吗?

main.py

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get('https://orlando.craigslist.org/search/cta')
owl = driver.find_element_by_xpath('//*[@id="sortable-results"]/ul/li/p/a')
res = 1
size = len(driver.find_elements_by_xpath('//*[@id="sortable-results"]/ul/li/p/a'))
def run():
    for i in range(0, size):
        owl = driver.find_elements_by_xpath('//*[@id="sortable-results"]/ul/li/p/a')
        owl[i].click().send_keys(Keys.COMMAND + 't')
        driver.find_element_by_xpath('/html/body/section/header/nav/ul/li[3]/p/a').click()
        if i == 1:
            break

if __name__ == '__main__':
    run()

这是错误

Traceback (most recent call last):
  File "main.py", line 24, in <module>
    run()
  File "main.py", line 17, in run
    owl[i].click().send_keys(Keys.COMMAND + 't')
AttributeError: 'NoneType' object has no attribute 'send_keys'

根据我使用.net上的selenium webdriver的经验,.click()不返回元素。它什么都没有。它导致单击一个元素,在这种情况下,这将导致在驱动程序的当前实例中加载一个新页面。我认为这就是这里发生的事情。

我建议作为替代方案,您应该在所有这些中收集所有" href"属性;a>数组中的元素,然后为每个元素打开一个新选项卡,然后使用答案在此处使用答案如何使用Selenium Web Driver在新选项卡(Chrome)中打开链接?。打开新标签并切换到它后,请使用driver.get()加载页面。

最新更新