I've trying to find the element 'Search' in the code above, but everytime I receive Unable to locate element. I already tried with xpath and css
<div class="wrapper-customer-search">
<search-customers class="hydrated">
#shadow-root (open)
<input type="text" placeholder="Search">
</search-customers>
you can try using io.github.sukgu that helps you to work on the shadow elements. I was able to automate the scenario that you mentioned. Below is the detailed code.
Step 1 add the below dependency
<!-- https://mvnrepository.com/artifact/io.github.sukgu/automation -->
<dependency>
<groupId>io.github.sukgu</groupId>
<artifactId>automation</artifactId>
<version>0.1.3</version>
</dependency>
Step 2 use the below import in the test file
import io.github.sukgu.*;
Step 3 Below is the entire code that worked for me
WebDriver driver = new ChromeDriver();
driver.get("<your_webpage>");
Shadow shadow = new Shadow(driver);
WebElement searchField = shadow.findElement("input[placeholder='Search']");
The <input> element with placeholder value as Search is within #shadow-root (open).
Solution
To locate the element you have to use shadowRoot.querySelector() and you can use the following solution:
Code Block:
time.sleep(3)
element = driver.execute_script("""return document.querySelector('search-customers').shadowRoot.querySelector("input[placeholder='Search']")""")
Reference
You can find a couple of relevant detailed discussions in:
How to handle the popup "Accepting all cookies" when the element is data-testid - Using Selenium in Python
Related
I'm trying to type text in a text box
<input name="pesquisarUtente.numeroUtente" class="number numero-utente" id="pesquisarUtente.numeroUtente" type="text" maxlength="9" value="">
I've tried the following
driver.find_element_by_name("pesquisarUtente.numeroUtente").send_keys('123123123')
driver.find_element_by_id("pesquisarUtente.numeroUtente").send_keys('123123123')
I always get the error:
selenium.common.exceptions.NoSuchElementException: Message: Unable to find element with css selector == [id="pesquisarUtente.numeroUtente"]
Try using Xpaths or Css Selectors.
This error commonly occurs when there is more than one element with the same id so selenium is expecting find_elementS_by_id.
Xpaths and Selectors are unique to each element so they work better when trying to find a specific element.
example using xpaths:
driver.find_element_by_xpath("ENTER XPATH HERE").send_keys('123123123')
To get the Xpath of an element you simply 1: hover over the element in the developer mode 2:right click on it 3:hover over the copy option 4:move down to copy Xpath and click
Css Selectors work the same way. (driver.find_element_by_css_selector("SELECTOR HERE"))
And you can see the option to copy one 3 above the option to copy the Xpath
I get an 'Element Not Visible Exception' when trying to click on a Search option on a webpage. The element is not hidden, and I have put a time.sleep(10) so the page has enough time to load. Please suggest why I am getting this error and how I can get around it.
I want to click on 'New Search' option in the code.
<a class="newsearch btn btn3d tbbtn" href="javascript:" style="position:static">
<div id="TBnewsearch"><img src="../../../../resources/images/mt_sprites.gif"
alt="New search" />
</div>
<span>New search</span>
</a>
Please find my code for clicking on it below :
time.sleep(10)
New_Search = browser.find_element_by_css_selector(' #Toolbar > table > tbody > tr > td.TBGroup.TBGroup1 > a.newsearch.btn.btn3d.tbbtn')
action2 = ActionChains(browser)
action2.move_to_element(New_Search).click()
action2.perform()
I've also tried doing a simple find and click on the element but get the same exception at the New_Search.click() step.
time.sleep(15)
New_Search = browser.find_element_by_xpath('//*[#id="Toolbar"]/table/tbody/tr/td[2]/a[1]')
New_Search.click()
I've tried using WebDriverWait as suggested by Debanjan below, but the expected condition isn't satisfied and I get a timeout exception.
time.sleep(15)
WebDriverWait(browser, 15).until(EC.visibility_of_element_located(browser.find_element_by_xpath('//*[#id="Toolbar"]/table/tbody/tr/td[2]/a[1]')));
New_Search.click()
Can you test these locators in the Dev tools and check if they are pointing to the right element on the page? In the console use $x() to test XPaths and $$() to test CSS selectors. If it returns 0, you know there's something wrong with your locator. If it returns 1, you are good to go. If it returns more than 1, you will need to verify that the element you are looking for is the first element returned. If it's not, you will need to craft a new locator. I have a feeling that your current locators is returning 2 nodes, out of which the first one is invisible.
Try to access the target element using that elements ancestors. Keep going one level up till you find a node that is uniquely identifiable and then use relative xpath (//) to traverse down to your element. You can also use ordinals/index to locate the element, but i would not recommend it as they will make your test brittle. If possible update the question with a bigger snippet of the HTML or the applications URL
I'm using Selenium with python to make a spider.
A part of the web page is like this:
text - <span class="sr-keyword">name</span> text
I need to find the href and click.
I've tried as below:
target = driver.find_element_by_class_name('_j_search_link')
target.click()
target is not None but it doesn't seem that it could be clicked because after target.click(), nothing happened.
Also, I've read this question: Click on hyperlink using Selenium Webdriver
But it can't help me because in my case, there is a <span class>, not just a simple text Google.
You can look for an element with class _j_search_link that contains text Wat Chedi Luang
driver.find_element_by_xpath('//a[#class="_j_search_link" and contains(., "Wat Chedi Luang")]')
driver.find_elements_by_partial_link_text("Wat Chedi Luang").click()
I think you didn't find right element. Use CSS or Xpath to target this element then click it, like:
//a[#class='_j_search_link']
or
//a[#class='_j_search_link']/span[#class='sr-keyword']
So, currently I'm using Python 3 and the selenium webdriver with Salesforce to automate admin verifications.
I've been pretty successful (even though I'm not that proficient with programming). However, I've run into an issue... I can't seem to figure out how to find an element on the page so that I can verify the text contained within is accurately being displayed.
This is what it looks like on the user's end:
The highlighted element displays as this
But whenever I search for "GlobalHeaderCommunitySwitcher", it spits back an error that it can't find it.
So I try searching for the other elements in the block of code:
<b class="zen-selectArrow"></b>PVT GBI Internal
<b class="zen-selectArrow"></b>
"PVT GBI Internal"
I've come up empty each time by searching by:
browser.find_element_by_id("globalHeaderCommunitySwitcher")
browser.find_element_by_class_name & used "zen-trigger" and "zen-selectArrow"
browser.find_element_by_xpath("//div[#class='zen-trigger'and text()='PVT GBI Internal']")
This also results in nothing being returned..
Essentially, how do I locate the element in the screenshot via the above code and then have the script verify that the text within that element ("PVT GBI INTERNAL") is present and correct?
you can use //tag[text()="value"] or //tag[contains(attribute,‘value’)]
example : browser.find_element_by_xpath("//a[#class='zen-trigger']//*[text()='PVT GBI Internal']")
//a[#class='zen-trigger']//*[contains(text(),'PVT GBI Internal')]
//a[#class='zen-trigger']//*[contains(#class="zen-selectArrow")and
contains(text(),'PVT GBI Internal')]
Open the page using the google chrome browser
Move the mouse over the element that you want to find and right-click it
Left click Inspect (at the bottom of the selection list)
Your element will be hi-lighted in the Developers tools
Right click the hi-lighted element and select Copy
Click either copy Selector or XPath depending on your preference
Paste that into your selenium find_element_by_xpath() or find_element_by_css_selector() statement as appropriate.
Say xpath
element = browser.find_element_by_xpath("your pasted xpath")
assert element.text == 'Your expected text'
I have a button
<input type="submit" class="button button_main" style="margin-left: 1.5rem;" value="something">
I cannot find it by id or name and need to submit a form.
I tried doing this:
Alternatively, WebDriver has the convenience method “submit” on every element. If you call this on an element within a form, WebDriver will walk up the DOM until it finds the enclosing form and then calls submit on that. If the element isn’t in a form, then the NoSuchElementException will be raised:
element.submit()
http://selenium-python.readthedocs.org/navigating.html
But that cannot find the submit selector either.
ideas?
There are many options here, to name a few:
If class alone is unique, you can use
driver.find_element_by_css_selector(".button_main").click()
If class + value combo is unique, you can use:
driver.find_element_by_css_selector(".button_main[value='something']").click()
You can also use xpath:
driver.find_element_by_xpath("//input[#type='submit' and #value='something']").click()
If none of those work (i.e. they are not identifying button uniquely), look at the elements above the button (for example <form) and provide the xpath in format:
driver.find_element_by_xpath("//unique_parent//input[#type="submit" and #value='something']").click()
i recommend xpath chrome extension, with it you will be able to get the path by running the extension and shift-clicking on the element you want this chrome extension https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl
You could try to find the element with an XPath expression or a CSS selector like input[type="button"], and then just click the element.