如果您想了解如何逐步在PythonSelenium中向下滚动和selenium向下滑动的知识,那么本篇文章将是您的不二之选。我们将深入剖析如何逐步在PythonSelenium中向下滚动的各个方面,并
如果您想了解如何逐步在Python Selenium中向下滚动和selenium向下滑动的知识,那么本篇文章将是您的不二之选。我们将深入剖析如何逐步在Python Selenium中向下滚动的各个方面,并为您解答selenium向下滑动的疑在这篇文章中,我们将为您介绍如何逐步在Python Selenium中向下滚动的相关知识,同时也会详细的解释selenium向下滑动的运用方法,并给出实际的案例分析,希望能帮助到您!
本文目录一览:- 如何逐步在Python Selenium中向下滚动(selenium向下滑动)
- Click()函数在python / Selenium中不起作用
- Python selenium代码向下滚动成功但id无法打印推文内容
- Python+Selenium中级篇之-Python读取配置文件内容
- Python+Selenium中级篇之-二次封装Selenium中几个方法
如何逐步在Python Selenium中向下滚动(selenium向下滑动)
嗨,大家好,我是Selenium和Python的新手。我只是在抓取站点pagalguy网站。我知道如何向下滚动到页面底部,但是我需要逐步向下滚动,以便Selenium单击所有readmore按钮,但是我不知道如何逐步向下滚动,因此我像下面的一个一样硬编码
browser.execute_script("window.scrollTo(0,300);")browser.find_element_by_link_text("Read More...").click()browser.execute_script("window.scrollTo(300,600);")browser.find_element_by_link_text("Read More...").click()browser.execute_script("window.scrollTo(600,900);")browser.find_element_by_link_text("Read More...").click()browser.execute_script("window.scrollTo(900,1200);")browser.find_element_by_link_text("Read More...").click()browser.execute_script("window.scrollTo(1200,1500);")browser.find_element_by_link_text("Read More...").click()browser.execute_script("window.scrollTo(1500,1800);")browser.find_element_by_link_text("Read More...").click()browser.execute_script("window.scrollTo(1800,2100);")browser.find_element_by_link_text("Read More...").click()browser.execute_script("window.scrollTo(2100,2500);")browser.find_element_by_link_text("Read More...").click()it goes on .......
我尝试使用while循环将其自动化,但它导致错误,上面的一个可行,但是我希望它简短并循环,以便可以将其用于具有不同页面长度的所有其他页面。
initial_value = 0next_value = 300while next_value<300000: browser.execute_script("window.scrollTo(initial_value,next_value);") browser.find_element_by_link_text("Read More...").click() initial_value=next_value next_value+=300
JavascriptException:消息:ReferenceError:没有定义initial_value
但是我已经定义了值,我想我已经解释了我实际上要做什么,我想自动向下滚动并单击所有readmore按钮,然后将获得全文内容
答案1
小编典典同意@Rahul Chawla的回答。
但是增加了一项更改。你可以试试这个
driver = webdriver.Chrome()read_mores = driver.find_elements_by_xpath(''//a[text()="Read More..."]'')for read_more in read_mores: driver.execute_script("arguments[0].scrollIntoView();", read_more) driver.execute_script("$(arguments[0]).click();", read_more)
Click()函数在python / Selenium中不起作用
请参阅我对您发布的问题的评论。最后,我确实找到了您的按钮,但是如果不使用JavaScript,就无法点击它:
from selenium import webdriver
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
try:
driver.implicitly_wait(6) # wait up to 6 seconds to find an element
driver.get("https://www.nike.com/ch/fr/launch?s=upcoming")
loginBtns = driver.find_elements_by_xpath("/html/body/div[2]/div/div/div[1]/div/header/div[1]/section/div/ul/li[1]/button")
if loginBtns:
#loginBtns[0].click()
driver.execute_script('arguments[0].click()',loginBtns[0])
else:
print('Could not find login button.')
finally:
driver.quit()
Python selenium代码向下滚动成功但id无法打印推文内容
首先,您的定位器不正确。使用 CSS 选择器。
第二,如果你想循环打印文本,把它放在那里。 最后:您需要等待您的元素变得可见。
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
import time
import re
import os
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import selenium.webdriver.support.ui as ui
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
def find_hashtags(hashtag):
driver.get('https://twitter.com/hashtag/' + hashtag + '?src=hash')
body = driver.find_element_by_tag_name('body')
wait = ui.WebDriverWait(driver,5)
while True:
try:
wait.until(
EC.visibility_of_element_located((By.XPATH,"//span[contains(@class,'Icon Icon--large Icon--logo')]")))
print("error")
break
except:
body.send_keys(Keys.END)
wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,".css-901oao.r-18jsvk2.r-1qd0xha.r-a023e6.r-16dba41.r-rjixqe.r-bcqeeo.r-bnwqim.r-qvutc0>span:nth-of-type(1)")))
tweets = driver.find_elements_by_css_selector('.css-901oao.r-18jsvk2.r-1qd0xha.r-a023e6.r-16dba41.r-rjixqe.r-bcqeeo.r-bnwqim.r-qvutc0>span:nth-of-type(1)')
for tweet in tweets:
print(tweet.text)
find_hashtags("any hashtag")
另外,请注意 0.5
秒等待时间太短。
Python+Selenium中级篇之-Python读取配置文件内容
本文来介绍下Python中如何读取配置文件。任何一个项目,都涉及到了配置文件和管理和读写,Python支持很多配置文件的读写,这里我们就介绍一种配置文件格式的读取数据,叫ini文件。Python中有一个类ConfigParser支持读ini文件。
1. 在项目下,新建一个文件夹,叫config,然后在这个文件夹下新建一个file类型的文件:config.ini
文件内容如下:
# this is config file, only store browser type and server URL
[browserType]
#browserName = Firefox
browserName = Chrome
#browserName = IE
[testServer]
URL = https://www.baidu.com
#URL = http://www.google.com
2. 百度搜索一下,python中如何获取当前项目的根目录的相对路径
这里采用:
os.path.dirname(os.path.abspath(''.''))
3. 在另外一个包下新建一个测试类,用来测试读取配置文件是否正常。
# coding=utf-8
import ConfigParser
import os
class TestReadConfigFile(object):
def get_value(self):
root_dir = os.path.dirname(os.path.abspath(''.'')) # 获取项目根目录的相对路径
print root_dir
config = ConfigParser.ConfigParser()
file_path = os.path.dirname(os.path.abspath(''.'')) + ''/config/config.ini''
config.read(file_path)
browser = config.get("browserType", "browserName")
url = config.get("testServer", "URL")
return(browser,url) # 返回的是一个元组
trcf = TestReadConfigFile()
print trcf.get_value()
你可以试试更改config.ini的内容,看看测试打印出来是不是你更改的东西,在配置文件一般#表示注释,你想要哪行配置代码起作用,你就把前面的#去除,并且在注释其他同一个区域。在ini文件中 中括号包裹起来的部分叫section,了解一下就可以。
Python+Selenium中级篇之-二次封装Selenium中几个方法
本文来介绍,如何把常用的几个webdriver的方法封装到自己写的一个类中去,这个封装过程叫二次封装Selenium方法。我们把打开站点,浏览器前进和后退,关闭和退出浏览器这这个方法封装到一个新写的类中去。
我们按照如下层次结构在PyCharm中新建两个包和两个.py文件:
上图,baidu_search.py是我们编写测试脚本的python文件,具体测试代码写在这个文件。包test1下的basepage.py文件是这次我们介绍的二次封装selenium方法而新建的。这里提一下,python中默认规则,包名和文件名都是小写,类名称单词首字母大写,函数名称小写,多个字母下划线隔开。我们尽量遵守下这个不成文的约定。
来看看basepage.py的内容:
上面的''''''''''''是文档注释,一般在类的开始和函数的开始,用两个''''''''''''括起来,简单描述下这个类或者函数的功能。
接下来看看,我们脚本文件中如何去调用我们自己封装过的方法。
baidu_search.py的内容如下:
上面self.basepage的几行代码就是调用我们自己封装的方法去执行相关webdriver操作。这个只是一个简单的封装介绍,等后面,我们介绍了字符串切割,我们会再次介绍二次封装Selenium方法,例如将会把八大find_element方法封装到一个方法里去。
关于如何逐步在Python Selenium中向下滚动和selenium向下滑动的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Click()函数在python / Selenium中不起作用、Python selenium代码向下滚动成功但id无法打印推文内容、Python+Selenium中级篇之-Python读取配置文件内容、Python+Selenium中级篇之-二次封装Selenium中几个方法等相关知识的信息别忘了在本站进行查找喔。
本文标签: