<h3 _ngcontent-ydk-c51="" matline="" class="mat-line">Text1</h3>
<h3 _ngcontent-ydk-c51="" matline="" class="mat-line">Text2</h3>
There are many links with class ="mat-line", but i want to click on link which contains Text1, how to do it ?
To click on the element with text as Text1 you need to induce WebDriverWait for the elementToBeClickable() (Java) / element_to_be_clickable() (Python) and you can use either of the following Locator Strategies:
Using Java and xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//h3[#class='mat-line' and text()='Text1']"))).click();
Using Python and XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h3[#class='mat-line' and contains(., 'Text1')]"))).click()
Note: Using Python client you have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
To get an h3 class of mat-line with text Text1 try the following xpath.
//h3[contains(text(), "Text1") and #class="mat-line"]
Found these two xpath working for Selenium with C#
"//h3[contains(text(),'Text1')]"
"//*[#class='mat-line'][.='Text1']"
Related
I am trying to get the value #2011 which is a URL text from the HTML below. I tried the below code but didnt work. It says it is unable to locate the class
driver.find_element(By.XPATH, '//span[#class = "data-issue-and-pr-hovercards-enabled"]').get_attribute('a')
Can anyone help to correct the mistake? I am new to selenium.
<span data-issue-and-pr-hovercards-enabled>
<span><span> · Fixed by #2011</span><span></span></span>
</span>
Here is the link to the website - github.com/mlpack/mlpack/issues/2008 I want to get the #2011 which is next to the Fixed by Text (below the title of the issue). Is it possible to do this?
Try the below XPath:
This relative xpath will search for all tag names(*) which contains the text "#2011"
//*[contains(text(),'#2011')]
Or try the below one: Very similar explanation as above but this will search only within <a> tag
//a[contains(text(),'#2011')]
Update:
Try the below XPath:
//span[contains(text(),'Fixed by')]//a
Use .text method to fetch the required value. This will get you the below text value.
In the given HTML data-issue-and-pr-hovercards-enabled is an attribute but not the value of class.
To extract the text #2011 ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:
Using CSS_SELECTOR and text attribute:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span[data-issue-and-pr-hovercards-enabled] a[data-hovercard-type='pull_request'][data-hovercard-url='/mlpack/mlpack/pull/2011/hovercard']"))).text)
Using XPATH and get_attribute("innerHTML"):
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[#data-issue-and-pr-hovercards-enabled]//a[#data-hovercard-type='pull_request' and #data-hovercard-url='/mlpack/mlpack/pull/2011/hovercard']"))).get_attribute("innerHTML"))
Note : You have to add the following 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 find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python
References
Link to useful documentation:
get_attribute() method Gets the given attribute or property of the element.
text attribute returns The text of the element.
Difference between text and innerHTML using Selenium
Trying to submit the check in form for United Airlines (https://www.united.com/en/us/checkin). I can't get the continue button to automate. The xpath is dynamic so is css selector. Can't find a name or id browser to find. How can I find this element? Never coded before so this is all I have been able to pickup from youtube
html for button
<button class="app-components-Button-styles__button--LbfHO app-components-Button-styles__tertiary--20H47 app-components-Button-styles__contained--2kXyi" type="submit">Continue</button>
Code so far:
from selenium import webdriver
browser = webdriver.Chrome('/Users/jeff/chromedriver/chromedriver')
browser.get('https://www.united.com/en/us/checkin')
browser.find_element('name', 'confirmationNumberModel.number').send_keys('AAAA1')
browser.find_element('name', 'confirmationNumberModel.lastName').send_keys('LastName')
browser.implicitly_wait(3)
Try to locate button by XPath:
browser.find_element(By.XPATH, '//button[.="Continue"]').click()
You can also call submit method from any element inside form node:
browser.find_element('name', 'confirmationNumberModel.lastName').submit()
P.S. Note that browser.implicitly_wait(3) should be called before locating elements
Given the HTML:
<button class="app-components-Button-styles__button--LbfHO app-components-Button-styles__tertiary--20H47 app-components-Button-styles__contained--2kXyi" type="submit">Continue</button>
All the classnames are dynamically generated. So we can't use them and have to look out for the static element attributes.
Solution
To click on Continue you can use either of the following locator strategies:
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "div[title='Confirmation or eTicket number'] button[type='submit']").click()
Using xpath:
driver.find_element(By.XPATH, "//button[#type='submit' and text()='Continue']").click()
I am trying to find cases using Xpath/Selenium where there is no white-space before the hyperlink.
e.g.
<p>Click on thislinkto access the data</p>`
This renders as Click on thislinkto access the data
Problem : Locate all the <a> elements and test if they have white-space before and after
Is there any elegant way to get the text before/after of anchor? I am thinking of using XPath query such as //a/parent::* which returns <p> element but the <a> tag is not separated. Can I somehow get the immediate text before/after the anchor tag?
Since you're using selenium, I'm assuming xpath 1.0.
This should select a elements that don't have a preceding or following space...
//a[substring(preceding-sibling::text()[1],string-length(preceding-sibling::text()[1]) )!=' ' or substring(following-sibling::text()[1],1,1)!=' ']
As per the HTML, there are two text nodes with text Click on this and to access the data and a <a> element within their parent element.
<p>
Click on this
link
to access the data
</p>
Solution
To print text before the <a> element you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using Python, XPATH, childNodes and strip():
print(driver.execute_script('return arguments[0].firstChild.textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(., 'link')]//ancestor::p[1]")))).strip())
Using Python, XPATH, get_attribute() and splitlines():
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(., 'link')]//ancestor::p[1]"))).get_attribute("innerHTML").splitlines()[1])
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I'm facing an issue to select the options from a list drop down box in Selenium.
Below is the list box html sample and the Dropbox box.
<li class="MuiButtonBase-root MuiListItem-root MuiMenuItem-root Mui-selected MuiMenuItem-gutters MuiListItem-gutters MuiListItem-button Mui-selected"
tabindex="0" role="option" aria-disabled="false" aria-selected="true" data-value="588ad39c8557bd23e3c16059">
(public) ISO<span class="MuiTouchRipple-root"></span></li>
<li class="MuiButtonBase-root MuiListItem-root MuiMenuItem-root MuiMenuItem-gutters MuiListItem-gutters MuiListItem-button" tabindex="-1"
role="option" aria-disabled="false" data-value="611e693ad6018009c8f7c681">
(public) public.dwg<span class="MuiTouchRipple-root"></span></li>
I have tried it using select element and unfortunately the select class is not in my home code hence it is not working.
So I would like to check if there is anyway I could make the Dropbox to be selected without using a select class.
Any help will be appreciated. Hope someone answers this question.
Note: have searched Google and tried many solutions and at the end none of them was working.
Selenium Select class is for drop down built using Select and Option tag. The htmnl that you've shared is using li tag so Select will not work.
Fix :
Click on drop down bar.
Click on the desired option.
Code :
Assuming this is in Python, You could do following :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "XPath of drop down bar"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[contains(text(),'(public) public.dwg')]"))).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
For this code need a css-selector, x-path is available
Following is the x-path such as:
//div[#class='item-display-name' and text()='edit']
HTML of the element:
<div class="item-display-name">edit</div>
To locate the element you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use the following css-selectors based Locator Strategy:
element = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.item-display-name")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Update
As per your comment, No you can't include the text edit within the css-selectors. You can find a detailed discussion in:
selenium.common.exceptions.InvalidSelectorException with “span:contains('string')”