Python-Selenium-WebDriver 页面正在滚动经过没有 "clicking" 的元素,即使驱动程序应该
Python-Selenium-WebDriver The page is scrolling past elements without "clicking" on them even though the driver is supposed to
我正在做一个项目,在 Firefox 中使用 Python 和 Selenium WebDriver,以便打开 Google,搜索特定项目,然后让 Selenium 打开前 5 个不同的搜索结果选项卡。
我想通过使用 selenium 复制 Ctrl Button Down-->Click Link-->Ctrl Button Up
来做到这一点。我在编写“Click
操作时遇到的问题是元素不在 ViewPort 中,无法点击这些元素。所以我添加了 Move_to_Element 操作,问题仍然存在(但它确实打开了前 2 或 3 个链接)。然后我使用元素的位置作为参考添加了 window.scroll_to
脚本,但现在它不打开任何 links。浏览器打开,它只是滚动在 link 之后到 link 直到到达最后一个。
你能帮我弄清楚我在这里做错了什么吗,因为逻辑看起来很好,并且在添加 scroll_to
操作之前有效(至少在前 2-3 links).
非常感谢
#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
循环中的最后一行,
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
我正在做一个项目,在 Firefox 中使用 Python 和 Selenium WebDriver,以便打开 Google,搜索特定项目,然后让 Selenium 打开前 5 个不同的搜索结果选项卡。
我想通过使用 selenium 复制 Ctrl Button Down-->Click Link-->Ctrl Button Up
来做到这一点。我在编写“Click
操作时遇到的问题是元素不在 ViewPort 中,无法点击这些元素。所以我添加了 Move_to_Element 操作,问题仍然存在(但它确实打开了前 2 或 3 个链接)。然后我使用元素的位置作为参考添加了 window.scroll_to
脚本,但现在它不打开任何 links。浏览器打开,它只是滚动在 link 之后到 link 直到到达最后一个。
你能帮我弄清楚我在这里做错了什么吗,因为逻辑看起来很好,并且在添加 scroll_to
操作之前有效(至少在前 2-3 links).
非常感谢
#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
循环中的最后一行,
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