单击元素时页面自动滚动

Page auto scrolls upon clicking an element

提问人:Rishe 提问时间:12/24/2021 最后编辑:Rishe 更新时间:12/24/2021 访问量:37

问:

嗨,大家好,我真的找不到为什么当我单击文本框时页面会自动滚动

def launchBrowser():
   options =ChromeOptions()
   options.add_argument("window-size=1280,1024")
   options.add_experimental_option('excludeSwitches', ['enable-logging'])
   driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)
   driver.get("xxxxxxxxxxxxxxxxxxxxxxxxxx")
   return driver 
driver = launchBrowser()
by_xpath = driver.find_element_by_xpath

以上是我用于浏览器的选项。我将 Chrome 驱动程序管理器与 Python selenium 一起使用。

iam面临的问题是,当我点击元素发送键时,页面会自动向上滚动。

下面是我编写的代码。

time.sleep(2)
toClear = by_xpath("//*[contains(@placeholder ,'Weight')]");
toClear.send_keys(Keys.CONTROL + "a");
toClear.send_keys(Keys.DELETE);
by_xpath("//*[contains(@placeholder ,'Weight')]").send_keys(120)

当我尝试单击下面的元素时,我面对自动滚动,我也尝试向下滚动。

driver.execute_script("window.scrollTo(0, 800)")
time.sleep(2)
by_xpath("//*[contains(@placeholder ,'L')]").click()
toClear = by_xpath("//*[contains(@placeholder ,'L')]");
toClear.send_keys(Keys.CONTROL + "a");
toClear.send_keys(Keys.DELETE);
by_xpath("//*[contains(@placeholder ,'L')]").send_keys(45)

请让我知道我做错了什么 请将代码视为单个实体

python selenium-chromedriver chrome-web-driver 进行 Web 测试

评论

1赞 Iain Shelvington 12/24/2021
有什么功能,可以分享一下吗?by_xpath
0赞 Rishe 12/24/2021
by_xpath = driver.find_element_by_xpath

答: 暂无答案