Good day everyone:
I’d like to get data from the url (sorry its encod, cause system always report its a spam)
after click the basketball then click the date combobox menu and select the first date …
Now I try to get all date from menu and wish to get the date format like yyyy/mm/dd. Below is the code
import time
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
driver = webdriver.Chrome()
driver.set_window_size(1500, 1350)
# open url (sorry for the url , cause system always report its a spam)
url1 = "http"
url2 = "888.net/spo"
url3 = "s://web2.sa8"
url4 = "rt/Games.a"
url5 = "evice=pc"
url6 = "spx?lang=3&d"
driver.get(url1 url3 url2 url4 url6 url5)
# jump to basketball
locator = (By.XPATH, '//*[@id="menuList"]/div/ul/li[3]/div[2]/a[1]')
pointer = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(locator),
"element not found"
)
actions = ActionChains(driver)
actions.click(pointer).perform()
time.sleep(1)
# date menu
locator = (By.XPATH, '//*[@id="chooseDate"]')
pointer = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(locator),
"element not found"
)
actions = ActionChains(driver)
actions.click(pointer).perform()
# jump to date 1
locator = (By.XPATH, '//*[@id="dateOption"]/a[2]/span[1]') # a[2] for second date, because first has no matches
pointer = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(locator),
"element not found"
)
actions = ActionChains(driver)
actions.click(pointer).perform()
# close AD by double clicl
locator = (By.ID, 'btn_close')
pointer = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(locator),
"element not found"
)
actions = ActionChains(driver)
actions.click(pointer).perform()
actions = ActionChains(driver)
actions.click(pointer).perform()
# wait for date
locator = (By.XPATH, './/*[@id="dateOption"]')
pointer = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(locator),
"element not found"
)
_dates = driver.find_elements(By.XPATH, './/*[@id="dateOption"]/a[*]')
for date in _dates:
#date_text = date.find_element(By.XPATH, './/*[@id="dateOption"]/a[*]')
#_date_text = date.find_element(By.ID, './/*[@id="displayDate"]')
_date_text = date.find_element(By.XPATH, './/div[@rel="20*"]')
print(_date_text.text)
time.sleep(120)
driver.quit()
and I got the error message
/usr/local/bin/python3 "/Users/andy/Python/sa8888 #5.3 test.py"
andy@Andys-iMac Python % /usr/local/bin/python3 "/Users/andy/Python/sa8888 #5.3 test.py"
Traceback (most recent call last):
File "/Users/andy/Python/sa8888 #5.3 test.py", line 78, in <module>
_date_text = date.find_element(By.XPATH, './/div[@rel="20*"]')
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 735, in find_element
return self._execute(Command.FIND_CHILD_ELEMENT,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 710, in _execute
return self._parent.execute(command, params)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":".//div[@rel="20*"]"}
(Session info: chrome=96.0.4664.110)
I am sure the HTML has the rel date data that I need
What should I do to fix it?
Thanks in advance
CodePudding user response:
All you need to do here is to extract the rel
attribute values from _dates
list of web elements.
So instead of
_dates = driver.find_elements(By.XPATH, './/*[@id="dateOption"]/a[*]')
for date in _dates:
#date_text = date.find_element(By.XPATH, './/*[@id="dateOption"]/a[*]')
#_date_text = date.find_element(By.ID, './/*[@id="displayDate"]')
_date_text = date.find_element(By.XPATH, './/div[@rel="20*"]')
print(_date_text.text)
You can do this:
_dates = driver.find_elements(By.XPATH, './/*[@id="dateOption"]/a[*]')
for date in _dates:
rel = date.get_attribute("rel")
print(rel)