Selenium Script Fails even though Xpath , Firebug show the correct element - selenium

I had a question . When i "inspect" this particular element and take "exact" xpath and copy it to my selenium script and run my script, it fails to identify?
Any idea how to do it?
Repeating again, i copied the exact xpath , tried inspecting element. All correct. Still :(
Thanks,
S.K

If Selenium fails to find an element you know is present, commonly the problem is with synchronization: Selenium tries to access the element too fast, before it appears on the page (and when you try to inspect element even a second later, you can see it, since it was rendered by then). Try to WAIT for the very same element before doing anything else. Examples of the wait can be found here

Related

Getting "Nosuch element Exception" in Selenium Though XPATH is correct. Not sure is this due to Shadow DOM. Please confirm

I am trying to automate Salesforce application Using Selenium and getting NoSuchelementException though XPATH is correct and valid for particular object. When i have searched the issue, it might be reason for Shadow DOM.
For EX:
So XAPTH i have written like,
driver.findElement(By.xpath("//input[#name='Name']")).sendKeys("Jams");
or
driver.findElement(By.xpath("//input[#id='input-299']")).sendKeys("Jams");
This XPATH is highlighting in Console as well. But while automating it throws nosuchelement error.
So while checking for ShadowDOM option, am getting option like this for Name Object.
#shadow-root(user-agent)
Shadowroot DIV
-- nothing mentioned in div. it just open and closed tags.
How to automate this?
You can check if there are any iframes in your Dom. Simply do //iframe in your page developer mode(F12)> elements tab > search (Ctrf+F) area. If there are any, you will get the number of iframes.
Now if your textbox is in any of iframe use below code to go inside particular iframe first
driver.switch_to.frame("<name or Id of frame>")
then come out to frame use below:
driver.switch_to.parent_frame()
Also, if the issue is not related to frames check below for shadow-root related issue:
you can check below for shadow-root element ( Question is for Java, but you can co-relate):
How to interact with the elements within #shadow-root (open) while Clearing Browsing Data of Chrome Browser using cssSelector
The website contents are mostly created now using javascript. You may have to wait for certain elements to load before doing anything to it.
https://seleniumbyexamples.github.io/wait

selenium webdriver takes very long when not finding an element in firefox

I am using selenium webdriver to locate element in firefox/win10.
The webpage is the following.
https://24h.pchome.com.tw/
There is an iframe tag in the html source document whose element can be accessed with the following two xpaths.
"//iframe[#src='https://www.googletagmanager.com/ns.html?id=GTM-TKSM5PF']"
'//html/body/noscript[1]/iframe[1]'
Then I used the following the python statement to locate the element once for each of the above xpaths.
e = WebDriverWait(driver, 1).until(EC.visibility_of_element_located((By.XPATH, xPath)))
The situation is that when a tag can be located, webdriver will return the element quickly. But for the above-mentioned element, somehow webdriver could not locate it and takes like 5 mins to return.
As you can see, the waiting time for the location is set to 1 second. But in fact, it takes like 5 mins.
I will appreciate it very much if someone can tell me how to make webdriver returns quickly when locating an element fails.
Thanks!
Farn
Sorry for the stupid question. As Guy suggested, this is because that the webdriverwait.until statement interfered with the implicitly_wait() statement declared somewhere else. After changing the implicitly_wait statement, the problem is gone.

Robot Framework - Locating input element with accept attribute fails

I am writing an automation script for an avatar upload module with the following CSS locator:
input[accept="image/png,image/jpeg,image/gif,image/bmp"]
I am using Robot Framework's Wait Until Element Is Visible keyword to look for the locator above but is unsuccessful with the error:
Element 'css=input[accept="image/png,image/jpeg,image/gif,image/bmp"]' not visible after 30 seconds.
Increasing the timeout also doesn't work. Using the same in Chrome Dev Tools would successfully find the element. My guess is that the commas/slashes are messing with Robot's locator parsing. My question is: What is the correct way to write the locator?
Though present in the DOM, an element may not be visible/rendered. This is very often the case with file upload input elements - the UI renders something different, a button, div that had applied styling and fits in better with the overall design.
Thus a check is it visible will rightfully fail. Change your pre-usage approach to validate the input is in the HTML - this is actually the same as what you did in the browser's dev tools - with the Page Should Contain Element keyword, and proceed on success.
There is no problem with the CSS locator your are using. Maybe the element is in another iframe?

Selenium cssselector shows correct webelement but script always run exception no such element

I got stuck not just for this one case. Many cases came out with nosuchelement exception, I cannot find the reason why. I use chrome console to locate the element and it shows correct result. Is that possible something wrong with the page itself, not my script?
Context click on the element you are trying to click, and see if the element is inside any frame. Try to navigate through the tags and see if this element is under any iframe.
switch to the iframe using,
driver.switchTo().frame(driver.findElement(By.xpath("iframexpath")));

Codeception ElementNotVisibleException error, unable to select option, or click

I am unable to interact with an element using browser tests. It says the element is not interact-able, or not visible. This doesn't happen in Acceptance
Sometimes this solution doesn't work because the element is unavailable for some other cryptic reason.
We just had a situation where we couldn't use a <select> element to pick one of the options.
Further more, there was behaviour that was being triggered by the "change" event when the option was selected.
We were able to solve it like this.
$js = "jQuery('#chosen-option-quantity-2').val('2').trigger('change');";
$I->executeJS($js);
so the first command selects the option, and the second triggers the change event.
I hope that helps some one, even if it is me in the future.
The problem that is happening here is that the html element is being hidden by something, probably css somewhere. Because it is hidden (display:none), WebDriver can't see it, and therefore can't interact with it.
In order to fix this problem, you need to use JS to un-hide the element.
use this $I->executeJS('jQuery("#your-css-selector").show()');
This doesn't happen in Acceptance tests because PHP Browser looks at the Page Source, and so can see everything, while WebDriver see's what a user see's on the browser.
You may use PhpBrowser
It works only with HTML then how PhantomJs emulate the real browser
But, with PhpBrowser you can't see what see your browser (only HTML such I said)
Another way, try executeJs with PhantomJs as it said before