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')”
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
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
<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']"
I'm trying to locate a web element that have two child nodes as:
<div _ngcontent-c2="" class=" " title="Twelve (Start date is 31| 0 user)" xpath="1"></div>
<div _ngcontent-c2="" class=" " title="Twelve (Start date is 31| 0 user)" xpath="2"></div>
I tried //div[contains(#title,'Twelve (Start date is 31| 0 user)')][1] to get the first element but didn't work for me.
xpath="1"
xpath="1" attribute is the reference of xpath v1.0
xpath="2"
xpath="2" attribute is the reference of xpath v2.0
For the record Selenium supports XPath v1.0 only.
A bit more of the outerHTML including the parent tag would have helped us to construct a more canonical answer. Moreover the information about the Language Binding you are using is also missing. However to locate the first element you need to induce WebDriverWait for the desired visibilityOfElementLocated() and you can use the following can use the following xpath:
Java:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(#title,'Start date is 31') and #xpath='1']")));
Python:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(#title,'Start date is 31') and #xpath='1']")))
Here you can find a detailed discussion on What are the differences between versions of XPath (1.0, 2.0, 3.1)
You can try with CSS:
div[xpath='1']
div[xpath='2']