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:
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()
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:
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.
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))
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))
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
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).
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