Home > Mobile >  Get the element by title inside repeatable class in Selenium Webdriver in Python
Get the element by title inside repeatable class in Selenium Webdriver in Python

Time:10-21

I want to click on an element which contains class and title in selenium python.

A webpage contains repeatable class without any id but with unique name. I want to detect and click on this title 'PaymateSolutions' once its loads in the page. Below is the html tag. I tried many ways but I am ending up with errors. Fyi I cant use the find element by class as they are not unique.

<div class="MuiGrid-root MuiGrid-item" title="PaymateSolutions">
<p class="MuiTypography-root jss5152 MuiTypography-body1">PaymateSolutions</p>
</div>

Few approaches that i tried to get driver element based on title using XPATH

Approach 1:-

wait = WebDriverWait(driver, 20)
element = wait.until(EC.element_to_be_clickable((By.XPATH,"//class[@title='PaymateSolutions']")))

Approach 2:-

element2 = (WebDriverWait(driver, 30).until(
            EC.visibility_of_element_located((By.XPATH, "//p[@title='PaymateSolutions']")))

        )

Approach 3:-

element2 = (WebDriverWait(driver, 30).until(
            EC.visibility_of_element_located((By.XPATH, "//[@title='PaymateSolutions']")))

        )

Can someone please help here?

CodePudding user response:

For Approach 1 - title is the attribute of div tag. So the Xpath would be something like below:

//div[@title='PaymateSolutions']

For Approach 2 - p tag has no title attribute. PaymateSolutions is the text of the p tag. Xpath should be something like this:

//p[text()='PaymateSolutions']

For Approach 3 - There is no Tag Name in the xpath. Xpath would be:

//*[@title='PaymateSolutions']
Or
//div[@title='PaymateSolutions']

Links to refer - Link1, Link2

We can apply Explicit waits like below:

# Imports required for Explicit waits:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver.get(url)

wait = WebDriverWait(driver,30)
payment_option = wait.until(EC.element_to_be_clickable((By.XPATH,"xpath for PaymateSolutions option")))
payment_option.click()

Link to refer for the Explicit waits - Link

CodePudding user response:

All the XPath that you've been trying seems a bit wrong. Please use the below XPath :

//div[@title='PaymateSolutions']//p[text()='PaymateSolutions']

Code trial 1:

time.sleep(5)
driver.find_element_by_xpath("//div[@title='PaymateSolutions']//p[text()='PaymateSolutions']").click()

Code trial 2:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@title='PaymateSolutions']//p[text()='PaymateSolutions']"))).click()

Code trial 3:

time.sleep(5)
button = driver.find_element_by_xpath("//div[@title='PaymateSolutions']//p[text()='PaymateSolutions']")
driver.execute_script("arguments[0].click();", button)

Code trial 4:

time.sleep(5)
button = driver.find_element_by_xpath("//div[@title='PaymateSolutions']//p[text()='PaymateSolutions']")
ActionChains(driver).move_to_element(button).click().perform()

Imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
  • Related