How to use selenium3.0.0 version to get the value of a button?

I want to use the selenium3.0.0 version to perform a login click test, but I have tried all the methods and can’t locate the damn button, because the website has done random id and other operations to prevent selenium from simulating login. This problem has confused me for a week. , can someone help me? as follows:

<a id="hibernate_cardRun_d6BlsWCSsQWwmECQbO9" href="https://******.********.********/*******************************************" target="_blank" rel="noopener noreferrer" data-mkt-id="console_main_button_runContainer" class="ContainerRunButton_ContainerRunButton__button__xLKfN IconTextButton_IconTextButton__k5VFn btn btn-outline-primary btn-md btn-block"><div class="_2pYHs _2Kwe0"><svg fill="currentColor" width="16" height="16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M5.10742 12.25V3.75L12.6074 8L5.10742 12.25Z"></path></svg><div class="ContainerRunButton_ContainerRunButton__msg__PbEMR">Run</div></div></a>

Instead of trying to using the id attribute, did you consider using the class attribute?

You could do something like:

wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.CLASS_NAME, "ContainerRunButton_ContainerRunButton__button__xLKfN")))

# then here you find the element by class name and click it
button = driver.find_element_by_class_name('ContainerRunButton_ContainerRunButton__button__xLKfN')
button.click()

Just a tip of advice, Selenium is kinda an old technology, you could use BeautifulSoup and Scraping Bee to have better results.

Scraping Bee have a free trial, if you are planning to use one time, they are great!

thks for reply,but the class name is radom…so, its unuseful. if selenium is not available, I will consider it.

If the class and the id are random you could try to find an attribute that is not random or a combination of them.

Hmmm, you can use XPath too. It’s a bit more complex but can get the job done. With XPath you can locate the <a> element that contains a <div> with class ContainerRunButton_ContainerRunButton__msg__PbEMR , with something like this:

wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.XPATH, "//a[.//div[contains(@class, 'ContainerRunButton_ContainerRunButton__msg__PbEMR')]]")))

button = driver.find_element_by_xpath("//a[.//div[contains(@class, 'ContainerRunButton_ContainerRunButton__msg__PbEMR')]]")
button.click()

The script waits until the button appear and XPath finds it and click it.

I tried to use xpath, but I only have the most basic xpath (copy the xpath address directly from the browser), but because the ID is random, it seems to be invalid, and the copied one is as follows:

//*[@id="hibernate_cardRun_d6BlsWCSsQWwmECQbO9"]

I will read your suggestion carefully, but it needs to be explained that I think this is invalid for random id, is there a better way?

That’s because, since the ID is random, using xpath or another thing will bring trouble. That’s why I said to use something that is static (will not change).

For example, in your snippet the button is an a tag, has a div with text “Run”.

# you always wait until the button is loaded on the page
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.XPATH, "//a[.//div[text()='Run']]")))

# and then find the element with XPath and click it
button = driver.find_element_by_xpath("//a[.//div[text()='Run']]")
button.click()

The xpath will be looking for an a tag that contains a div with text “Run”, and then clicks on the button.

2 Likes

because im used selenium 3.0.0 version,so when i test it, it prompts me “NameError: name ‘WebDriverWait’ is not defined” :sweat_smile:

Because the phantomjs I use, the new version of selenium does not support phantomjs, so…

Maybe the full code can get better help…

from selenium import webdriver
import time
import random
#import os
browser=webdriver.PhantomJS(executable_path="./phantomjs/bin/phantomjs")
browser.implicitly_wait(30)
browser.get("https://accounts.goorm.io/login?return_url=aHR0cHM6Ly9pZGUuZ29vcm0uaW8vbXkvZGFzaGJvYXJkP3JlZGlyZWN0PTE=") 
browser.set_window_size(1920,1080)
#rowser.find_element_by_id("emailInput").click()
#rowser.find_element_by_id("emailInput").clear()
browser.find_element_by_id("emailInput").send_keys("mail")
browser.find_element_by_id("passwordInput").send_keys("password")



from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = webdriver.Wait(browser, 10)
wait.until(EC.presence_of_element_located((By_XPATH, "//a[.//div[text()='Run']]")))

# and then find the element with XPath and click it
button = driver.find_element_by_xpath("//a[.//div[text()='Run']]")
button.click()


while True:
    browser.execute(webdriver.remote.command.Command.MOVE_TO,{
        "xoffset":random.randint(0,1920),
        "yoffset":random.randint(0,1080)
    })
    time.sleep(random.randint(0,10))

Oh there are some few things.

For example, you should import by in your code from selenium.webdriver.common.by import By

By is a class in Selenium that needs to be imported to locate the elements, so when you use By.XPATH the method will not be recognized.

And webdriver.wait should be wait = WebDriverWait(browser, 10). There is no dot between webdriverwait.

I think Selenium does not recognize By_XPATH too the correct term would be By.XPATH.

Let me rewrite this.

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import random

browser = webdriver.PhantomJS(executable_path="./phantomjs/bin/phantomjs")
browser.implicitly_wait(30)
browser.get("https://accounts.goorm.io/login?return_url=aHR0cHM6Ly9pZGUuZ29vcm0uaW8vbXkvZGFzaGJvYXJkP3JlZGlyZWN0PTE=") 
browser.set_window_size(1920,1080)

browser.find_element_by_id("emailInput").send_keys("mail")
browser.find_element_by_id("passwordInput").send_keys("password")

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(browser, 10)
wait.until(EC.presence_of_element_located((By.XPATH, "//a[.//div[text()='Run']]")))

# and then find the element with XPath and click it
button = browser.find_element_by_xpath("//a[.//div[text()='Run']]")
button.click()

while True:
    browser.execute(webdriver.remote.command.Command.MOVE_TO,{
        "xoffset":random.randint(0,1920),
        "yoffset":random.randint(0,1080)
    })
    time.sleep(random.randint(0,10))

2 Likes

it reported an error

Traceback (most recent call last):
  File "guaji.py", line 22, in <module>
    wait.until(EC.presence_of_element_located((By.XPATH, "//a[.//div[text()='Run']]")))
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Screenshot: available via screen

This isn’t helpfull But What is selenium?

I do not understand what you mean…

What is selenium? I allredy know it is python

Selenium automates browsers. That’s it!
uou can learn and use it from this website

1 Like

Try to increase the time wait to 30 seconds wait = WebDriverWait(browser, 30).

The error is that he’s not finding the specific parameters that we set (XPath is not matching the element)

It may be because the button is taking long to appear (that’s why I said to increase the time wait) or we need to find another element that is not random generate (I suggest look at the source code of the website and trying to find something that is always the same).

1 Like

I have checked it many times, but I did not find the same element as the mark point. The only thing is that the run button is the name of Run. I will try the above code. If possible, can you log in to my account to help me check if there is a way Find the element? No matter what the result is, thank you very much

1 Like

I tried to set the waiting time to 180 seconds, but the result is the same, so this method should have no effect

1 Like

pretty sure find_element_by_x is depricated use By syntax instead.

from selenium.webdriver.common.by import By

driver.find_element(By.XPATH, "")
1 Like