标签: execute-script

等到页面加载Selenium WebDriver for Python

我想刮掉由无限滚动实现的页面的所有数据.以下python代码有效.

for i in range(100):
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(5)
Run Code Online (Sandbox Code Playgroud)

这意味着每次向下滚动到底部时,我都需要等待5秒钟,这通常足以让页面完成加载新生成的内容.但是,这可能不是时间效率.页面可以在5秒内完成加载新内容.每次向下滚动时,如何检测页面是否已完成加载新内容?如果我能检测到这一点,一旦我知道页面加载完毕,我可以再次向下滚动以查看更多内容.这更节省时间.

python selenium execute-script

142
推荐指数
13
解决办法
27万
查看次数

Python和Selenium以"execute_script"来解决"ElementNotVisibleException"

我正在使用Selenium来保存网页.单击某些复选框后,网页内容将发生变化.我想要的是单击一个复选框,然后保存页面内容.(复选框由JavaScript控制.)

首先我用过:

driver.find_element_by_name("keywords_here").click()
Run Code Online (Sandbox Code Playgroud)

它以错误结束:

NoSuchElementException
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用"xpath",使用隐式/显式等待:

URL = “the url”

verificationErrors = []
accept_next_alert = True

aaa = driver.get(URL)
driver.maximize_window()
WebDriverWait(driver, 10)

#driver.find_element_by_xpath(".//*[contains(text(), ' keywords_here')]").click()
#Or: 

driver.find_element_by_xpath("//label[contains(text(),' keywords_here')]/../input[@type='checkbox']").click()
Run Code Online (Sandbox Code Playgroud)

它给出了一个错误:

ElementNotVisibleException
Run Code Online (Sandbox Code Playgroud)

帖子

如何强制Selenium WebDriver点击当前不可见的元素?

Selenium元素不可见异常

建议它应该在点击之前使复选框可见,例如使用:

execute_script
Run Code Online (Sandbox Code Playgroud)

这个问题可能听起来很愚蠢,但是如何从页面源代码中找到"execute_script"复选框可见性的正确句子?

除此之外,还有另一种方式吗?

谢谢.

顺便说一句,行html代码看起来像:

<input type="checkbox" onclick="ComponentArt_HandleCheck(this,'p3',11);" name="keywords_here">
Run Code Online (Sandbox Code Playgroud)

它的xpath看起来像:

//*[@id="TreeView1_item_11"]/tbody/tr/td[3]/input
Run Code Online (Sandbox Code Playgroud)

javascript python selenium selenium-webdriver execute-script

4
推荐指数
1
解决办法
2万
查看次数