Issue
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?
Solution
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']
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
Answered By - pmadhu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.