When using selenium to drive chrome to find certain elements, the website cannot be found. It is a course learning platform.

after I log in to the website through selenium, I want to start automatically clicking some buttons on the web page. Through xpath positioning, I can"t find

. The

code is as follows (account password is not important, you need to log in to enter the course, leaving it easy for everyone to help debug)

from selenium import webdriver
import time

-sharp chrome
opt = webdriver.ChromeOptions()

-sharp chromewindowslinux
-sharpopt.set_headless()

-sharp chrome
driver = webdriver.Chrome(options=opt)

driver.get("https://www.ulearning.cn/umooc/user/login.do")

-sharp
-sharpprint(driver.page_source)
-sharp 
user = driver.find_element_by_xpath("//*[@id="loginName"]")
user.send_keys("20164045033")
-sharp 
password = driver.find_element_by_xpath("//*[@id="password"]")
password.send_keys("dk154310")
-sharp 
bt = driver.find_element_by_xpath("//*[@id="loginForm"]/input[3]").click()
print(driver.page_source)

-sharpprint(driver.get_cookies())
-sharp cookies = {
-sharp "domain": "www.ulearning.cn",
-sharp     "httpOnly": False,
-sharp     "name": "staticCookie",
-sharp     "path": "/ulearning_web",
-sharp     "secure": True,
-sharp     "value": "1"
-sharp }
-sharp

-sharp 
time.sleep(3)
-sharp 
learn = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div/div[1]/div[1]/div/div/div/section[1]/div[2]/div[1]/div[5]/input").click()
time.sleep(10)
test = driver.find_element_by_xpath("//*[@id="page559654"]/div/div/span").click()

is that the element of the last line can not be found, it feels like it is loaded with javascript subclass, but I will not, please take a look at it and give the solution, thank you!


Native: chrome 64 python2.7 selenium3.8
gives you a try in headless mode. After logging in, the system detects that the browser version is too low or incompatible, so the location is not correct.
as shown in the following figure

you can try to take a screenshot after logging in

< H1 > Screenshot < / H1 >

driver.get_screenshot_as_file (time.strftime ("% Y%m%d%H%M%S") + ".png")


-sharp 
learn = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/div[1]/div[1]/div/div/div/section[1]/div[2]/div[1]/div[5]/input').click()

change this to:

learn = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/div[1]/div[1]/div/div/div/section[1]/div[2]/div[1]/div[5]/input');
learn.click();

it is more accurate to write xpath by yourself, and it may be more concise to use css. Please refer to the document

.
  1. css, https://saucelabs.com/resourc.
  2. xpath, https://www.w3schools.com/xml.
< hr >

the following is the reference code, which has been tested

-sharp -*- coding: utf-8 -*-
from selenium import webdriver
import time


driver = webdriver.Chrome()
driver.get('https://www.ulearning.cn/umooc/user/login.do')

-sharp 
user = driver.find_element_by_xpath('//*[@id="loginName"]')
user.send_keys('20164045033')
-sharp 
password = driver.find_element_by_xpath('//*[@id="password"]')
password.send_keys('dk154310')
-sharp 
bt = driver.find_element_by_xpath('//*[@id="loginForm"]/input[3]').click()

-sharp 
time.sleep(3)
driver.find_element_by_css_selector('div.progress_bg div.right input').click()

time.sleep(3)
driver.find_element_by_css_selector('button.btn-hollow').click()

time.sleep(3)
driver.find_element_by_css_selector('div.close-btn').click()

time.sleep(1)
items = driver.find_elements_by_css_selector('div.page-icon + span')
items[1].click()

time.sleep(1)
driver.find_element_by_css_selector('-sharpalertModal .modal-operation .btn-submit').click()

-sharp 
time.sleep(1)
driver.find_element_by_css_selector('.jwdisplayIcon .jwicon').click()
Menu