简体   繁体   English

Selenium IDE python 代码导出,不起作用

[英]Selenium IDE python code export, doesnt work

I want to use Selenium IDE to do some easy tasks in Chrome an then export the code to python and execute there.我想使用 Selenium IDE 在 Chrome 中执行一些简单的任务,然后将代码导出到 python 并在那里执行。 However, when I execute the exported code in python nothing happens.但是,当我在 python 中执行导出的代码时,什么也没有发生。

# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class TestIDETest():
 
  def setup_method(self, method):
    self.driver = webdriver.Chrome('C:/Users/Jakob/Documents/Python-Selenium/chromedriver.exe')
    self.vars = {}
  
  def teardown_method(self, method):
    self.driver.quit()
    
  
  def test_iDETest(self):
    self.driver.get("https://www.google.de/")
    self.driver.set_window_size(1050, 660)
    self.driver.find_element(By.NAME, "q").send_keys("wikipedia")
    self.driver.find_element(By.NAME, "q").send_keys(Keys.ENTER)
    self.driver.find_element(By.CSS_SELECTOR, ".rc:nth-child(3) .LC20lb").click()
    self.driver.find_element(By.ID, "txtSearch").click()
    self.driver.find_element(By.ID, "txtSearch").send_keys("delta")
    self.driver.find_element(By.ID, "txtSearch").send_keys(Keys.ENTER)
    self.driver.find_element(By.LINK_TEXT, "English").click()
    element = self.driver.find_element(By.CSS_SELECTOR, ".mw-wiki-logo")
    actions = ActionChains(self.driver)
    actions.move_to_element(element).perform()
    element = self.driver.find_element(By.CSS_SELECTOR, "body")
    actions = ActionChains(self.driver)
    actions.move_to_element(element, 0, 0).perform()
    element = self.driver.find_element(By.CSS_SELECTOR, "#ca-talk > a")
    actions = ActionChains(self.driver)
    actions.move_to_element(element).perform()
    self.driver.close()

When I delete the class and methods and just execute the "raw" code it works.当我删除 class 和方法并只执行“原始”代码时,它就可以工作了。 So I am not sure, how to exactly work with those methods.所以我不确定如何准确地使用这些方法。 Thanks in advance.提前致谢。

If you want this to run as-is, you need to instantiate the class and kick off the functions inside.如果您希望它按原样运行,您需要实例化 class 并启动其中的功能。 Add this at the end of the script:在脚本末尾添加:

testClass = TestIDETest()

testClass.setup_method("")
testClass.test_iDETest()
testClass.teardown_method("")

I've not exported from the IDE before so I can't comment on the purpose of the "method" variable that it wants (and it doesn't seem to do anything with it) - but it works as blank.我之前没有从 IDE 导出,所以我无法评论它想要的“方法”变量的目的(而且它似乎没有做任何事情) - 但它作为空白工作。

There is also some more reading here about setting up these classes to run a pyTest test.这里还有更多关于设置这些类以运行 pyTest 测试的阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM