0

I want to run the game on this page 100 times, and each time, pull out the final metric:

Step: 1000 x = (7,1,9,10) a = (0) y = (7,1,8,10) Bad steps: 148 Good steps: 852 Score: 85.20% Run over.

I can see where the line is in the console:

here

So I wrote this:

from selenium import webdriver
import os
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import pandas as pd
import time
import sys
import re
import requests

options = Options()
#options.binary_location=r'/usr/local/bin/'
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_argument("--headless")
driver = webdriver.Chrome(options=options,executable_path='/usr/local/bin/chromedriver')
output_file = open('output.txt', 'a')

driver.get('https://run.ancientbrain.com/run.php?world=complex&mind=complex')
field = driver.find_element_by_xpath('user_span1')
print(field.text)

The error is:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"user_span1"}

I understand it's saying that it can't find the tag I'm looking for, could someone demonstrate how to pull out the text in the tag I'm looking for (user_span1 and user_span2)? I want to test my code (not shown here, not relevent) by running the code multiple times and comparing the bad steps/good steps/score to a different set of runs using different code parameters so I want to pull out this metric for a set of runs....I could do it manually, but I've been wanting to improve my selenium anyway.

p.s. I have seen similar questions, and I know how to do this when the span is static on the webpage with beautifulSoup etc, but when i click inspect on this page, the tag I want is not there, so I don't think it's a straightforward job with beautifulSoup/pandas/I haven't seen an answer that specifically answers this case.

1 Answer 1

1

user_span is the id of the <span> tag. So, you would use:

field = driver.find_element_by_id('user_span1')

If you are using Chrome and you have an arbitrary field you are interested in, you can open up the Inspector (right-mouse click and chose Inspect), click on the field you are interested in, then in the Inspector window right-mouse click on the element. On the pop-up menu select Copy and then Copy full XPath. You can then paste this string as the argument to a find_element_by_xpath call.

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks I was so close, appreciate it.
I've added some more info concerning XPath that might be of some use in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.