Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium-Python:滚动到find_element*方法找到的元素_Python_Selenium - Fatal编程技术网

Selenium-Python:滚动到find_element*方法找到的元素

Selenium-Python:滚动到find_element*方法找到的元素,python,selenium,Python,Selenium,我开发了一个selenium脚本,可以在facebook群组中自动发表评论。 它工作得相对较好,但是如果目标元素在浏览器上不可见,它不会执行click()方法。 作为一种解决方法,我使用的是执行脚本(“window.scrollTo(x,y)”),但它不是理想的脚本。必须改进的代码如下: text_box = driver.find_element_by_class_name("UFIAddCommentInput") try: d

我开发了一个selenium脚本,可以在facebook群组中自动发表评论。 它工作得相对较好,但是如果目标元素在浏览器上不可见,它不会执行
click()
方法。 作为一种解决方法,我使用的是执行脚本(“window.scrollTo(x,y)”),但它不是理想的脚本。必须改进的代码如下:

text_box = driver.find_element_by_class_name("UFIAddCommentInput")                            
try:
    driver.execute_script("window.scrollTo(100, 0);")
    text_box.click()
except:
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    text_box.click()
element = driver.switch_to.active_element
element.send_keys(frase)
element.send_keys(Keys.RETURN)
它首先尝试页面顶部的元素,如果没有执行
click()
,则在底部尝试。 但是,有一种更有效的方法可以滚动通过
find\u element\u by\u class\u name
方法找到的元素。

您可以尝试

text_box.location_once_scrolled_into_view
text_box.click()

将页面向下向右滚动到所需元素并单击它

,您的解决方案似乎工作得很好。这是最好的解决方案。谢谢!