Python Selenium WebDriver页面滚动通过元素时不带;点击";即使司机应该

Python Selenium WebDriver页面滚动通过元素时不带;点击";即使司机应该,python,selenium,selenium-webdriver,python-requests,python-3.7,Python,Selenium,Selenium Webdriver,Python Requests,Python 3.7,我正在做一个项目,在Firefox中使用Python和Selenium WebDriver,以便打开Google,搜索特定项目,然后让Selenium在不同的选项卡中打开前5个搜索结果 我想通过使用selenium复制Ctrl按钮向下-->单击链接-->Ctrl按钮向上来实现这一点。我在编写“单击操作时遇到的问题是,元素不在视口中,无法单击。因此,我添加了“移动到元素”操作,问题仍然存在(但它确实打开了前2或3个链接)。然后我添加了窗口。使用元素的位置作为参考,滚动到脚本,但现在它不会打开任何链

我正在做一个项目,在Firefox中使用Python和Selenium WebDriver,以便打开Google,搜索特定项目,然后让Selenium在不同的选项卡中打开前5个搜索结果

我想通过使用selenium复制
Ctrl按钮向下-->单击链接-->Ctrl按钮向上
来实现这一点。我在编写“
单击
操作时遇到的问题是,元素不在视口中,无法单击。因此,我添加了“移动到元素”操作,问题仍然存在(但它确实打开了前2或3个链接)。然后我添加了
窗口。使用元素的位置作为参考,滚动到
脚本,但现在它不会打开任何链接。浏览器打开,它只会滚动链接,直到到达最后一个链接

你能帮我弄清楚我在这里做错了什么吗,因为逻辑似乎很好,而且在添加
滚动\u到
操作(至少在前2-3个链接中)之前它工作正常

非常感谢

#TO-DO open google
browser = webdriver.Firefox()
browser.get("https://www.google.com")

#Selecting the search bar and send search text
searchElem = browser.find_element_by_css_selector('.gLFyf')
searchElem.send_keys("cars")
searchElem.submit()
time.sleep(5)
last_height = browser.execute_script("return document.body.scrollHeight")
# browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")

found_elems=browser.find_elements_by_class_name('LC20lb')

#Selecting and clicking on first 5 pages
idx = 0

while idx <= min(len(found_elems),5):
    found_elem = found_elems[idx]

    #Find the height of the element
    ht = found_elem.location['y']

    print("Opening up ",found_elem.text)#Page Name
    try:
        print("In try block")

        #Scrolling to the element
        browser.execute_script("window.scrollTo(0, {});".format(ht))

        #Setting up Action Chains to move to elem-> Click on the links #with ctrl key down so as to open them in different tabs 
        ActionChains(browser)\
        .move_to_element(found_elem)\
        .key_down(Keys.CONTROL) \
        .click(found_elem) \
        .key_up(Keys.CONTROL) \
        .perform()
        print("Browser moved to "+str(ht))
        print("Exiting try")

    except Exception as e:
        print("In exception")
        print(e)
        break
    idx + = 1
#待办事项打开谷歌
browser=webdriver.Firefox()
browser.get(“https://www.google.com")
#选择搜索栏并发送搜索文本
searchElem=browser.find_element_by_css_选择器('.gLFyf'))
searchElem.发送钥匙(“汽车”)
searchElem.submit()
时间。睡眠(5)
last\u height=browser.execute\u脚本(“return document.body.scrollHeight”)
#browser.execute_脚本(“window.scrollTo(0,document.body.scrollHeight);”)
found_elems=浏览器。按类名称(“LC20lb”)查找元素
#选择并单击前5页
idx=0
而idx则在按住ctrl键的同时单击链接#,以便在不同的选项卡中打开它们
动作链(浏览器)\
.将元素移动到元素(找到元素)\
.key_向下(Keys.CONTROL)\
。单击(找到元素)\
.按键向上(按键控制)\
.perform()
打印(“浏览器移动到“+str(ht))
打印(“退出尝试”)
例外情况除外,如e:
打印(“例外”)
打印(e)
打破
idx+=1

循环中的最后一行

idx + = 1

语法不正确。它应该是
idx+=1
。因此,我不知道如何摆脱循环,它将继续使用
idx=0
进行迭代,我是否遗漏了什么?

循环中的最后一行

idx + = 1

语法不正确。它应该是
idx+=1
。因此我不知道您如何摆脱循环,它只会继续使用
idx=0
进行迭代,我缺少什么吗?

如果您计划在新选项卡中打开搜索链接。下面是简化的代码

browser.get("https://www.google.com")

#Selecting the search bar and send search text
searchElem = browser.find_element_by_css_selector('.gLFyf')
searchElem.send_keys("cars")
searchElem.submit()
time.sleep(5)

found_elems=browser.find_elements_by_xpath("//*[@class='LC20lb']/parent::a")

#Selecting and clicking on first 5 pages
idx = 0

while idx <= min(len(found_elems),5):
    found_elem = found_elems[idx]
    # scroll to link
    found_elem.location_once_scrolled_into_view

    print("Opening up ",found_elem.text)#Page Name
    try:
        # opening the link in new tab
        browser.execute_script("window.open('"+found_elem.get_attribute('href')+"')")
    except Exception as e:
        print(e)
        break
    idx =idx+1
browser.get(“https://www.google.com")
#选择搜索栏并发送搜索文本
searchElem=browser.find_element_by_css_选择器('.gLFyf'))
searchElem.发送钥匙(“汽车”)
searchElem.submit()
时间。睡眠(5)
find_elems=browser.find_elements_by_xpath(“//*[@class='LC20lb']]/parent::a”)
#选择并单击前5页
idx=0

而idx如果您计划在新选项卡中打开搜索链接,那么下面是简化的代码

browser.get("https://www.google.com")

#Selecting the search bar and send search text
searchElem = browser.find_element_by_css_selector('.gLFyf')
searchElem.send_keys("cars")
searchElem.submit()
time.sleep(5)

found_elems=browser.find_elements_by_xpath("//*[@class='LC20lb']/parent::a")

#Selecting and clicking on first 5 pages
idx = 0

while idx <= min(len(found_elems),5):
    found_elem = found_elems[idx]
    # scroll to link
    found_elem.location_once_scrolled_into_view

    print("Opening up ",found_elem.text)#Page Name
    try:
        # opening the link in new tab
        browser.execute_script("window.open('"+found_elem.get_attribute('href')+"')")
    except Exception as e:
        print(e)
        break
    idx =idx+1
browser.get(“https://www.google.com")
#选择搜索栏并发送搜索文本
searchElem=browser.find_element_by_css_选择器('.gLFyf'))
searchElem.发送钥匙(“汽车”)
searchElem.submit()
时间。睡眠(5)
find_elems=browser.find_elements_by_xpath(“//*[@class='LC20lb']]/parent::a”)
#选择并单击前5页
idx=0

idx是你的整个脚本吗?只是想确认你已经导入了键和动作链。欢迎使用。是强制使用ctrl+click还是只想在新选项卡中打开链接?嗨,是的,我已经导入了键和动作链。不是强制使用ctrl+click,我只需要在新选项卡中打开。这是你的整个脚本吗?只想确认您已经导入了键和动作链。欢迎使用SO。是强制使用ctrl+click还是只想在新选项卡中打开链接?您好,是的,我已经导入了键和动作链。不强制使用ctrl+click,我只需要在新选项卡中打开。我认为这是一个打字错误。否则会引发错误,因为我t在try blockhi之外,是的,事实上这是一个输入错误。在上传之前必须格式化代码,显然没有做得很好xD。我认为这是一个输入错误。否则会抛出一个错误,因为它在try blockhi之外,是的,实际上是一个输入错误。在上传之前必须格式化代码,并且没有做得很好显然是xD。