GVKun编程网logo

使用Selenium和python在textBox中快速编写(textbox python)

12

本文将为您提供关于使用Selenium和python在textBox中快速编写的详细介绍,我们还将为您解释textboxpython的相关知识,同时,我们还将为您提供关于AttributeError:

本文将为您提供关于使用Selenium和python在textBox中快速编写的详细介绍,我们还将为您解释textbox python的相关知识,同时,我们还将为您提供关于AttributeError:“列表”对象使用Selenium和Python没有属性“单击”、python – 使用Selenium和Firefox版本40,我如何下载文件?、Selenium3.4.0-Python3.6.1:在使用unittest的Selenium- Python绑定中,我如何决定何时使用self.assertIn或assert、Selenium和Python查找元素和文本?的实用信息。

本文目录一览:

使用Selenium和python在textBox中快速编写(textbox python)

使用Selenium和python在textBox中快速编写(textbox python)

我正在使用Selenium和Python(Chorme驱动程序)在文本框中编写内容,但是有很多文本框,我需要它来更快地填充它们。我使用一系列

driver.find_element_by_xpath("//input[@string required'' and @id=''order_billing_name'']").send_keys("test.com")

命令,但是写10-11这些则要花费很多时间。有办法加快速度吗?

答案1

小编典典

您可以尝试使用javascript设置值:

orderBillingName = driver.find_element_by_id("order_billing_name")driver.execute_script("arguments[0].value=arguments[1];", orderBillingName, "mytext")

您可以直接使用javascript进行全部设置:

driver.execute_script("document.querySelector(''#order_billing_name'').value=''mytext'';"\                   "document.querySelector(''.order_number_class'').value=''12323'';"\                   "document.querySelector(''input#anotherinputid'').value=''anything'';")

许多字段的示例:

fields = {    "input#order_billing_name": "some text",     ".order_number_class"     : "some text",     "css selector"            : "some text",     "css selector"            : "some text",    "css selector"            : "some text"    }js = ""for selector, value in fields.items():    js = js + "document.querySelector(''" + selector + "'').value=''"+ value +"'';"driver.execute_script(js)

AttributeError:“列表”对象使用Selenium和Python没有属性“单击”

AttributeError:“列表”对象使用Selenium和Python没有属性“单击”

我想在默认设置为“季度”的页面上单击“年度”按钮。有两个基本上相同的链接,除了一个链接,data- ptype="Annual"所以我试图复制xpath来单击按钮(也尝试了其他选项,但没有一个起作用)。

但是,我得到了AttributeError: 'list' object has no attribute 'click'。我读了很多类似的文章,但无法解决我的问题..因此,我认为必须调用/单击/执行javascript事件以某种方式不同。

from selenium import webdriver
link = 'https://www.investing.com/equities/apple-computer-inc-balance-sheet'

driver = webdriver.Firefox()
driver.get(link)
elm = driver.find_elements_by_xpath("/html/body/div[5]/section/div[8]/div[1]/a[1]").click()

html如下:

<ahref="javascript:void(0);" data-type="rf-type-button" data-ptype="Annual" data-pid="6408" data-rtype="BAL">..</a>

python – 使用Selenium和Firefox版本40,我如何下载文件?

python – 使用Selenium和Firefox版本40,我如何下载文件?

通过Selenium下载文件的旧方法似乎不再起作用.

我的代码是:

fp = webdriver.FirefoxProfile()
    fp.set_preference("browser.download.dir",os.getcwd())
    fp.set_preference("browser.download.folderList",2)
    fp.set_preference("browser.download.manager.showWhenStarting",False)
    fp.set_preference("browser.helperApps.neverAsk.savetodisk","application/pdf")

    self.driver = webdriver.Firefox(firefox_profile=fp)
    self.longMessage = True

但是,仍会显示文件对话框.我已经完成了相当多的切换字段,但经过一些挖掘后,我发现Selenium生成的默认Firefox配置文件的prefs.js文件与其中一个的prefs.js文件没有区别.我已经在下载对话框中手动选中了“从现在开始为此类文件自动执行此操作”.

mimeTypes.rdf文件确实发生了变化,具体而言,添加了下面的行:

<RDF:Description RDF:about="urn:mimetype:handler:application/pdf"
               NC:alwaysAsk="false"
               NC:savetodisk="true"
               NC:handleInternal="false">
<NC:externalApplication RDF:resource="urn:mimetype:externalApplication:application/pdf"/>

但是,我不知道在创建新的Firefox配置文件时设置自定义mimeTypes.rdf文件的方法.有谁有想法吗?

为了抢先任何人建议我只是下载URL,该文件是为用户生成的,我需要专门验证.pdf文件是否已下载到驱动器.

解决方法

我是一个R用户,所以只需使用 RSelenium在R中发布我的解决方案.如果你无法在python中转换相同的信息,请告诉我,我会暗示相同的.

kNown_formats <- c("application/vnd.ms-excel","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")


firefox_profile.me <- makeFirefoxProfile(list(marionette = TRUE,# this is for certificate issues [may be ignored]
                                              webdriver_accept_untrusted_certs = TRUE,webdriver_assume_untrusted_issuer = TRUE,# download related settings
                                              browser.download.folderList = 2L,browser.download.manager.showWhenStarting = FALSE,# put your path here but remember to give path like C:\\DirOfYourChoice and not C:\\DirOfYourChoice\\ [last \\ is not going to work]
                                              browser.download.dir = normalizePath("TestDL"),browser.helperApps.alwaysAsk.force = FALSE,browser.helperApps.neverAsk.openFile = paste0(kNown_formats,collapse = ","),browser.helperApps.neverAsk.savetodisk = paste0(kNown_formats,# this is for marionette and related security
                                              "browser.tabs.remote.force-enable" = TRUE,pdfjs.disabled = TRUE))

remDr <- remoteDriver(remoteServerAddr = "localhost",port = 4444,browserName = "firefox",extraCapabilities = firefox_profile.me)

remDr$open()

remDr$navigate("https://www.google.com/search?q=sample+xlsx")

remDr$findElement(using = "css selector",value = ".g:nth-child(1) a")$clickElement()

remDr$navigate("https://www.google.com/search?q=test+xls")

remDr$findElement(using = "css selector",value = ".g:nth-child(1) a")$clickElement()

和我一起工作
我正在使用

Firefox 50.1.0 [while I''m writing this post]
Selenium [3.0.1]
R [3.3.2 (2016-10-31)]

希望你能将这个移植到python.只是尝试在firefox makeFirefoxProfile中复制配置

参考进一步了解: –
How to Download files using Selenium
Firefox Profile Settings in Selenium

Selenium3.4.0-Python3.6.1:在使用unittest的Selenium- Python绑定中,我如何决定何时使用self.assertIn或assert

Selenium3.4.0-Python3.6.1:在使用unittest的Selenium- Python绑定中,我如何决定何时使用self.assertIn或assert

我正在使用Selenium 3.4.0和Python
3.6.1。我已经通过Python文档通过unittest模块编写了一个脚本,该模块是基于Java的内置Python,JUnit在Windows 8
Pro计算机,64位OS,x-64处理器上使用geckodriver 0.16.1和Mozilla Firefox
57.0。在我的测试方法test_search_in_python_org()中,以下几行效果很好:

    def test_search_in_python_org(self):      driver = self.driver      driver.get("http://www.python.org")      self.assertIn("Python", driver.title)      elem = driver.find_element_by_name("q")      elem.send_keys("pycon")      elem.send_keys(Keys.RETURN)      assert "No results found." not in driver.page_source

当我断言“页面标题”时,我正在使用: self.assertIn("Python", driver.title)

但是,当我声明一个字符串(我的假设)时,在页面源中使用的是: assert "No results found." not indriver.page_source

我的问题是什么因素/条件决定了我应该使用self.assertIn还是简单使用 assert

任何建议或指示将对您有所帮助。

答案1

小编典典

看一下Python unittest
文档,同时回顾一下我曾经在这里进行的一系列Django单元测试,这些都是我的发现。

使用案例:

第一件事是最简单的事情,而我认为两者之间最大的不同是可以使用每个命令的情况。在测试类的情况下,它们都可以互换使用,但是,要使用该assertIn命令,您需要导入unittest库。所以,说我想知道是否hhello。通过assertIn命令执行此操作的简单方法是:

class MyTestCase(unittest.TestCase):    def is_h_in_hello(self):        self.assertIn("h", "hello")

然后我需要运行测试,即通过unittest.main()本示例进行测试,以获得答案。但是,使用该assert命令可以更轻松地查看hin
hello。这样做非常简单:

assert "h" in "hello"

但本质上,两者都会给我相同的答案。但是,两种方法的区别在于第二种方法的使用简便。

结果:

我发现的第二个区别是结果在Python
Shell上的可读性。该unittest库的设计,使得命令是非常具体的。因此,如果测试失败,您将收到一条非常明确的消息,指出出了什么问题。现在说,你想看看是否bhello。通过class方法(只需更改"h""b")进行操作,运行测试后得到的消息是:

AssertionError: ''b'' not found in ''hello''----------------------------------------------------------------------Ran 1 test in 0.038sFAILED (failures=1)

因此,它非常清楚地告诉您:''b'' not found in''hello'',这使您很方便地查看问题所在。但是说您通过assert命令执行相同的过程。生成的错误消息类似于:

Traceback (most recent call last):  File "<pyshell#2>", line 1, in <module>    assert "b" in "hello"AssertionError

尽管它告诉您错误类型(AssertionError)和回溯,但并没有特别告诉您"b" is NOT in"hello"。在这种简单的情况下,很容易查看回溯并说哦,您好!但是,在更复杂的情况下,要弄清为什么会生成此错误消息可能会很棘手。

总体而言,这两种方法非常相似,可以为您提供所需的结果,但是从本质上讲,这归因于此处和此处的微小差异,更少的代码行以及Shell消息的直接性。

Selenium和Python查找元素和文本?

Selenium和Python查找元素和文本?

当我转到某个网页时,我正在尝试查找某个元素和一段文本:

<span>0</span>

这不起作用:( 它给出了复合类名的错误…)

elem = browser.find_elements_by_class_name("Bold Orange Large")

所以我尝试了这个:( 但是我不确定它是否有效,因为我不太了解在selenium中使用CSS选择器的正确方法…)

elem = browser.find_elements_by_css_selector("span[Bold Orange Large'']")

找到span元素后,我想找到其中的数字 (内容)

num = elem.(what to put here??)

CSS选择器,类名和查找元素文本的任何帮助都将非常有用!

谢谢。

哦! 我的另一个问题是,其中有多个确切的span元素,但内部有不同的数字。我该如何处理?

答案1

小编典典

您想要: elem.text 获得“里面的数字”。

说明:

在您的示例中,elem是webdriver
WebElement类的实例(来自selenium.webdriver.remote.webelement)

WebElement实例具有多个属性,包括:

  • tag_name
  • text
  • size
  • location
  • parent
  • id

.text 属性包含“内容”


“而且我的另一个问题是,这些确切的跨度元素有多个。我该如何处理?”

请不要一口气问几个问题..它不适用于SO的格式。

但基本上,使用find_elements_*代替find_element_*。然后,您的定位器可以返回匹配WebElement实例的列表,而不是单个匹配实例的列表……您可以对其进行进一步过滤。

今天关于使用Selenium和python在textBox中快速编写textbox python的介绍到此结束,谢谢您的阅读,有关AttributeError:“列表”对象使用Selenium和Python没有属性“单击”、python – 使用Selenium和Firefox版本40,我如何下载文件?、Selenium3.4.0-Python3.6.1:在使用unittest的Selenium- Python绑定中,我如何决定何时使用self.assertIn或assert、Selenium和Python查找元素和文本?等更多相关知识的信息可以在本站进行查询。

本文标签: