问题
I want to use Selenium IDE to do some easy tasks in Chrome an then export the code to python and execute there. However, when I execute the exported code in python nothing happens.
# 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. So I am not sure, how to exactly work with those methods. Thanks in advance.
回答1:
If you want this to run as-is, you need to instantiate the class and kick off the functions inside. 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.
There is also some more reading here about setting up these classes to run a pyTest test.
来源:https://stackoverflow.com/questions/63342522/selenium-ide-python-code-export-doesnt-work