I'm trying to write a bot using Selenium Python to play an online game of tic-tac-toe. I've scraped the XPATHS of the squares and placed them in variables. The bot is simple. It's just supposed to click random squares. I'll enhance the bot later. Right now, I just want to click elements, and I'm getting stuck by this line of code:
squares[square].click()
I get an attribute error in the traceback. I understand strings can't invoke the click() method, and usually, I would have something like this:
pickSquare = WebDriverWait(load_browser, 10).until(EC.element_to_be_clickable(By.XPATH, Tags.someSquare))
pickSquare.click()
But I've put all of my XPATH variables into an array that I need to iterate through, and I'm not sure how to use By and EC on an array as such. Below is the code I have so far.
class TestCase_PlayGame():
URL = "http://localhost:3000"
@pytest.fixture
def load_browser(self, browser):
browser.get(self.URL)
yield browser
def test_playGame(self, load_browser):
squares = [Tags.square1,Tags.square2,Tags.square3,
Tags.square4,Tags.square5,Tags.square6,
Tags.square7,Tags.square8,Tags.square9]
clickedSquares = []
random_square = randint(0,8)
time.sleep(10)
for i in clickedSquares:
if i == random_square:
self.test_playGame()
else:
clickedSquares.append(random_square)
squares[random_square].click()
Below is the traceback.
self = <TestCases.TestCase_PlayGame.TestCase_PlayGame object at 0x000001DA3F4139D0>
load_browser = <selenium.webdriver.firefox.webdriver.WebDriver (session="324ff7dc-195c-4bdf-9ceb-84bf978dfc66")>
def test_playGame(self, load_browser):
squares = [Tags.square1,Tags.square2,Tags.square3,
Tags.square4,Tags.square5,Tags.square6,
Tags.square7,Tags.square8,Tags.square9]
clickedSquares = []
random_square = randint(0,8)
time.sleep(10)
for i in clickedSquares:
if i == random_square:
self.test_playTTT()
else:
clickedSquares.append(random_square)
> squares[random_square].click()
E AttributeError: 'str' object has no attribute 'click'
TestCases\TestCase_PlayGame.py:52: AttributeError
