GVKun编程网logo

如何在Python中使用Selenium?(python-selenium)

13

如果您想了解如何在Python中使用Selenium?和python-selenium的知识,那么本篇文章将是您的不二之选。我们将深入剖析如何在Python中使用Selenium?的各个方面,并为您解

如果您想了解如何在Python中使用Selenium?python-selenium的知识,那么本篇文章将是您的不二之选。我们将深入剖析如何在Python中使用Selenium?的各个方面,并为您解答python-selenium的疑在这篇文章中,我们将为您介绍如何在Python中使用Selenium?的相关知识,同时也会详细的解释python-selenium的运用方法,并给出实际的案例分析,希望能帮助到您!

本文目录一览:

如何在Python中使用Selenium?(python-selenium)

如何在Python中使用Selenium?(python-selenium)

如何设置Selenium与Python一起使用?我只想用Python编写/导出脚本,然后运行它们。是否有任何资源?我尝试使用谷歌搜索,但是发现的东西要么是指Selenium(RC)的过时版本,要么是Python的过时版本。

答案1

小编典典

您是说Selenium WebDriver吗?…

先决条件 :根据您的操作系统安装Python

使用以下命令安装

pip install -U selenium

并在您的代码中使用此模块

from selenium import webdriver

您还可以根据需要使用以下许多内容

from selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import Selectfrom selenium.common.exceptions import NoSuchElementException

这是更新的答案

我建议您在没有IDE的情况下运行脚本…这是我的方法

  1. 使用IDE查找对象/元素的xpath
  2. 并使用find_element_by_xpath()。click()

下面的示例显示登录页面自动化

#ScriptName : Login.py#---------------------from selenium import webdriver#Following are optional requiredfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import Selectfrom selenium.common.exceptions import NoSuchElementExceptionbaseurl = "http://www.mywebsite.com/login.php"username = "admin"password = "admin"xpaths = { ''usernameTxtBox'' : "//input[@name=''username'']",           ''passwordTxtBox'' : "//input[@name=''password'']",           ''submitButton'' :   "//input[@name=''login'']"         }mydriver = webdriver.Firefox()mydriver.get(baseurl)mydriver.maximize_window()#Clear Username TextBox if already allowed "Remember Me" mydriver.find_element_by_xpath(xpaths[''usernameTxtBox'']).clear()#Write Username in Username TextBoxmydriver.find_element_by_xpath(xpaths[''usernameTxtBox'']).send_keys(username)#Clear Password TextBox if already allowed "Remember Me" mydriver.find_element_by_xpath(xpaths[''passwordTxtBox'']).clear()#Write Password in password TextBoxmydriver.find_element_by_xpath(xpaths[''passwordTxtBox'']).send_keys(password)#Click Login buttonmydriver.find_element_by_xpath(xpaths[''submitButton'']).click()

还有另一种可以找到任何对象的xpath的方式-

  1. 在firefox中安装Firebug和Firepath插件
  2. 在Firefox中打开URL
  3. 按F12打开Firepath开发人员实例
  4. 在浏览器窗格下面选择Firepath,然后选择“ xpath”选择
  5. 将鼠标光标移至网页上的元素
  6. 在xpath文本框中,您将获得对象/元素的xpath。
  7. 将粘贴xpath复制到脚本。

运行脚本-

python Login.py

您也可以使用CSS选择器代替xpath。在大多数情况下,CSS选择器比xpath快一点,并且通常比xpath更受首选(如果要与之交互的元素上没有ID属性)。

如果将光标移动到对象,Firepath也可以将对象的定位器捕获为CSS选择器。您必须更新代码,以使用等效的CSS选择器方法find-

find_element_by_css_selector(css_selector)

不要等待在Python中使用Selenium加载页面

不要等待在Python中使用Selenium加载页面

如何在页面完全加载之前让硒单击元素并刮取数据?我的互联网连接非常糟糕,因此有时有时需要花很长时间才能完全加载页面,这是否可以解决?

答案1

小编典典

ChromeDriver 77.0 (支持Chrome 77版本)现在支持 eager 作为 pageLoadStrategy

已解决的问题1902:支持优先页面加载策略[Pri-2]


如您所提,click on elements and scrape data before the page has fullyloaded在这种情况下,我们可以利用属性 pageLoadStrategy 。当Selenium默认加载页面/ URL时,它将遵循默认配置,
pageLoadStrategy 设置为 normal 。Selenium可以从不同的代码开始执行下一行代码 Documentreadiness state 。目前,Selenium支持3种不同的功能 Document readiness state
,我们可以通过 pageLoadStrategy 以下方式进行配置:

  1. none (未定义)
  2. eager (页面变为交互式)
  3. normal (完整的页面加载)

这是配置代码的代码块 pageLoadStrategy

from selenium import webdriverfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesbinary = r''C:\Program Files\Mozilla Firefox\firefox.exe''caps = DesiredCapabilities().FIREFOX# caps["pageLoadStrategy"] = "normal"  #  completecaps["pageLoadStrategy"] = "eager"  #  interactive# caps["pageLoadStrategy"] = "none"   #  undefineddriver = webdriver.Firefox(capabilities=caps, firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")driver.get("https://google.com")

在Python中使用Selenium上传文件

在Python中使用Selenium上传文件

是否可以在Python脚本中上传带有硒的文件附件?

在Python中使用Selenium单击/选择单选按钮

在Python中使用Selenium单击/选择单选按钮

如何解决在Python中使用Selenium单击/选择单选按钮?

使用CSS选择器或XPath value直接按属性选择,然后单击它。

browser.find_element_by_css_selector("input[type=''radio''][value=''SRF'']").click()
# browser.find_element_by_xpath(".//input[@type=''radio'' and @value=''SRF'']").click()

更正(但是OP应该学习如何在文档中查找)

  • 在Python绑定中,find_elements_by_css它不存在,称为find_elements_by_css_selector。一个人应该能够查看异常消息并在这里回顾文档并找出原因。
  • 注意之间的差异find_element_by_css_selectorfind_elements_by_css_selector?第一个找到第一个匹配的元素,第二个找到一个列表,因此您需要使用[0]进行索引。这是api文档。之所以使用后者,是因为我复制了您的代码,但我不应该这样做。

解决方法

我正在尝试从3个按钮的列表中进行选择,但是找不到选择它们的方法。以下是我正在使用的HTML。

<input name="pollQuestion" type="radio" value="SRF"> 
    <font face="arial,sans-serif" size="-1">ChoiceOne</font><br />
<input name="pollQuestion" type="radio" value="COM">
    <font face="arial,sans-serif" size="-1">ChoiceTwo</font><br />
<input name="pollQuestion" type="radio" value="MOT">
    <font face="arial,sans-serif" size="-1">ChoiceThree</font>

我可以使用以下代码找到它:

for i in browser.find_elements_by_xpath("//*[@type=''radio'']"):
     print i.get_attribute("value")

输出:SRF,COM,MOT

但我想选择ChoiceOne。(单击它)我该怎么做?

在Python中使用Selenium单击具有相同类名的所有元素

在Python中使用Selenium单击具有相同类名的所有元素

我正在尝试单击网页上的所有“喜欢”按钮。我知道如何单击其中之一,但我希望能够全部单击它们。它们具有相同的类名,但ID不同。

我是否需要创建某种列表,并告诉它单击列表中的每个项目?有没有写“全部单击”的方法?

这是我的代码的样子(我删除了登录代码):

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()
browser.set_window_size(650,700)
browser.get('http://iconosquare.com/viewer.php#/tag/searchterm/grid')

mobile = browser.find_element_by_id('open-menu-mobile')
mobile.click()
search = browser.find_element_by_id('getSearch')
search.click()
search.send_keys('input search term' + Keys.RETURN)

#this gets me to the page I want to click the likes
fitness = browser.find_element_by_css_selector("a[href*='fitness/']")
fitness.click()

#here are the different codes I've tried to use to click all of the "like buttons"

#tried to create a list of all elements with "like" in the id and click on all of them.  It didn't work.
like = browser.find_elements_by_id('like')
for x in range(0,len(like)):
    if like[x].is_displayed():
        like[x].click()

#tried to create a list by class and click on everything within the list and it didn't work.
like = browser.find_elements_by_class_name('like_picto_unselected')
like.click()

AttributeError: 'list' object has no attribute 'click'

我知道我无法单击列表,因为它不是单个对象,但是我不知道如何处理。

非常感谢您的帮助。

我们今天的关于如何在Python中使用Selenium?python-selenium的分享就到这里,谢谢您的阅读,如果想了解更多关于不要等待在Python中使用Selenium加载页面、在Python中使用Selenium上传文件、在Python中使用Selenium单击/选择单选按钮、在Python中使用Selenium单击具有相同类名的所有元素的相关信息,可以在本站进行搜索。

本文标签: