Issue
I am trying to find an element in a table to firstly scrape the text/number and then, if it meets criteria, click on it.
Here's an example page: https://www.oddschecker.com/horse-racing/wolverhampton/20:30/winner.
To do this I need to locate the selection name in the table row (class="diff-row evTabRow bc"
) and the book in the data cell (data-bk="PP"
).
selection = 'Aramis Grey'
book = 'PP'
I'm new to selenium and can find the individual elements but don't know how to combine the criteria for different elements.
Edit:
The original link is no longer available but trying the suggestions using this link https://www.oddschecker.com/horse-racing/kempton/20:30/winner gives me the following error:
Traceback (most recent call last):
File "C:/Users/Oddschecker v5.py", line 42, in <module>
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '[class="diff-row evTabRow bc"][data-bname="Blue Beret"] [data-bk="WH"]'))).click()
File "C:\Users\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 693, in _execute
return self._parent.execute(command, params)
File "C:\Users\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 400, in execute
self.error_handler.check_response(response)
File "C:\Users\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 236, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <td class="bc bs oi" data-bk="WH" data-odig="4.33" data-o="10/3" data-hcap="" data-fodds="12.0" data-best-ew="false" data-best-wo="false" data-ew-denom="5" data-ew-places="4">...</td> is not clickable at point (762, 864). Other element would receive the click: <div class="_3g1ozk">...</div>
(Session info: chrome=94.0.4606.61)
Stacktrace:
Backtrace:
Ordinal0 [0x015CD403+2479107]
Ordinal0 [0x01567D51+2063697]
Ordinal0 [0x014720C8+1056968]
Ordinal0 [0x014A2A59+1256025]
Ordinal0 [0x014A0D7B+1248635]
Ordinal0 [0x0149EB4D+1239885]
Ordinal0 [0x0149D9FD+1235453]
Ordinal0 [0x014933C7+1192903]
Ordinal0 [0x014B5973+1333619]
Ordinal0 [0x014931E6+1192422]
Ordinal0 [0x014B5A5A+1333850]
Ordinal0 [0x014C4CAF+1395887]
Ordinal0 [0x014B583B+1333307]
Ordinal0 [0x01491F94+1187732]
Ordinal0 [0x01492DE9+1191401]
GetHandleVerifier [0x0174F3F6+1531734]
GetHandleVerifier [0x017FE9AE+2249998]
GetHandleVerifier [0x016539AB+501003]
GetHandleVerifier [0x01652A29+497033]
Ordinal0 [0x0156D11D+2085149]
Ordinal0 [0x015711C8+2101704]
Ordinal0 [0x01571302+2102018]
Ordinal0 [0x0157AA71+2140785]
BaseThreadInitThunk [0x74FC6A14+36]
RtlInitializeExceptionChain [0x7777AD8F+143]
RtlInitializeExceptionChain [0x7777AD5A+90]
Solution
There's a accept cookies button.
There's a modal pop up windows
There's a X pop up.
Use Explicit waits.
Sample code :
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
wait = WebDriverWait(driver, 30)
driver.get("https://www.oddschecker.com/horse-racing/wolverhampton/20:30/winner")
try:
print('When we have Want to be kept in the loop? Modal pop up')
wait.until(EC.visibility_of_element_located((By.XPATH, "//span[text()='Not Now']/.."))).click()
except:
print("If not, pass it")
pass
try:
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.js-close-class"))).click()
print('Clicked on close button if it appears')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-testid='accept-button']"))).click()
print('Clicked on accept cookies button')
except:
pass
wait.until(EC.element_to_be_clickable((By.XPATH, "//tr[@data-bname='Aramis Grey']//td[@data-bk='PP']"))).click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
You can comment out this block, cause I have not seen often Want to be kept in the loop? modal pop up.
try:
print('When we have Want to be kept in the loop? Modal pop up')
wait.until(EC.visibility_of_element_located((By.XPATH, "//span[text()='Not Now']/.."))).click()
except:
print("If not, pass it")
pass
Also, to mention that once you click, I believe it is opening the link in new tab. if that is the case then, you'd have to switch to new tab as well :
driver.switch_to.window(driver.window_handles[1])
and then you can interact with new tab items.
Answered By - cruisepandey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.