Home > front end >  Find Element Using Two Criteria in Python Selenium
Find Element Using Two Criteria in Python Selenium

Time:09-29

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.

CodePudding user response:

The below XPath will help you to cover both criteria

//tr[@class='diff-row evTabRow bc' and @data-bname="Aramis Grey"]//td[@data-bk="PP"]

CodePudding user response:

To do this with CSS Selector you can use this locator:

[class="diff-row evTabRow bc"][data-bname="Aramis Grey"] [data-bk="PP"]

If you prefer XPath - this locator can be used

//tr[@class="diff-row evTabRow bc" and(@data-bname="Aramis Grey")]//td[@data-bk="PP"]

The more complete code to be used with python selenium can be something like this:

wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '[class="diff-row evTabRow bc"][data-bname="Aramis Grey"] [data-bk="PP"]'))).click()

or

wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, '//tr[@class="diff-row evTabRow bc" and(@data-bname="Aramis Grey")]//td[@data-bk="PP"]'))).click()

respectively.

CodePudding user response:

  1. There's a accept cookies button.

  2. There's a modal pop up windows

  3. There's a X pop up.

  4. 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.

  • Related