Selenium webdriver : I want to click on "Like 1.3M" image which is on the bottom of https://www.msn.com/en-in/weather/today - image

I want to click on "Like 1.3M" image which is on the bottom of https://www.msn.com/en-in/weather/today
i have tried multiple approches for clicking on "facebook like" image like switching to iframe , clicking on image in webdriver.
but none of them are working .

Since you have no specified any language I will use Python to answer your question.
Firstly you need to switch to iframe that contains the like button like:
driver.get("https://www.msn.com/en-in/weather/today")
iframe = WebDriverWait(driver, 30).until(expected_conditions.presence_of_element_located((By.XPATH, '//*[#id="fbcount"]/iframe')))
iframe.location_once_scrolled_into_view
driver.switch_to.frame(iframe)
and then click like:
driver.find_element_by_xpath('//*[#id="u_0_0"]/div/button').click()
and then switch to default content
driver.switch_to.default_content()
If you get NoSuchElementException please add some wait time.
Here is the full code
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
driver = webdriver.Chrome("/chromedriver/location")
driver.get("https://www.msn.com/en-in/weather/today")
iframe = WebDriverWait(driver, 30).until(expected_conditions.presence_of_element_located((By.XPATH, '//*[#id="fbcount"]/iframe')))
iframe.location_once_scrolled_into_view
driver.switch_to.frame(iframe)
time.sleep(3)
driver.find_element_by_xpath('//*[#id="u_0_0"]/div/button').click()
time.sleep(3)
driver.switch_to.default_content()
driver.close()
driver.quit()

Instead of click(), use the action class click().
WebElement likeFB=driver.findElement(By.xpath("//div[#id='u_0_0']/div/button"));
Actions act = new Actions(driver);
act.moveToElement(likeFB).click().build().perform();

Related

Best practices for importing multiple components from react-bootstrap?

For performance reasons the React-Bootstrap documentation suggests importing individual components rather than the whole library. So:
import Button from 'react-bootstrap/Button';
instead of
import { Button } from 'react-bootstrap';
My question is whether there is a limit to this when importing numerous components? i.e.
import Alert from 'react-bootstrap/Alert';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';
import ProgressBar from 'react-bootstrap/ProgressBar';
import Popover from 'react-bootstrap/Popover';
import OverlayTrigger from 'react-bootstrap/OverlayTrigger';
import Button from 'react-bootstrap/Button';
import Badge from 'react-bootstrap/Badge';
as opposed to:
import { Alert, Row, Col, ProgressBar, Popover, OverlayTrigger, Button, Badge } from 'react-bootstrap';
Is there still such a performance gain that it's better to have the individual import statements, or is there a cutoff that others follow, where more than a certain number of components gets included in one line instead of multiple?
Just use named imports and tree shake not used code. For example with Create-React-App tree shaking is automatically configured. Then using
import { Alert, Row, Col, ProgressBar, Popover, OverlayTrigger, Button, Badge } from 'react-bootstrap';
is in the end the same as
import Alert from 'react-bootstrap/Alert';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';
import ProgressBar from 'react-bootstrap/ProgressBar';
import Popover from 'react-bootstrap/Popover';
import OverlayTrigger from 'react-bootstrap/OverlayTrigger';
import Button from 'react-bootstrap/Button';
import Badge from 'react-bootstrap/Badge';
To anwser your question directly: Yes, it always better to get rid of not used code. Even if you use ten components from react-bootstrap - this library has A LOT more and I can't imagine a single component where you would use even 70-80% of them.

Altair and Panel: Save as button not showing

I am using the Python library Altair to plot and let the user save as SVG file. To import data and interactive configuration I wanted to use the Panel library. They integrate well but the button to save is missing when I put the Altair plot inside a Panel element. Other interactivity works fine.
This seems to be a general problem as I can find no images of Altair plots inside a Panel element having the save button. However, I cannot find anybody addressing this specific issue, so the question is there any way to resurrect the button so the user can save?
(I know about altair_saver for programmatically saving, but unfortunately we cannot allow the dependencies on our system)
Here is a simple example of Altair with and without Panel.
And here is the code:
import pandas as pd
import altair as alt
alt.__version__
import panel as pn
pn.extension('vega')
pn.__version__
dictdata = {'variable': ['A', 'B', 'C', 'D'], 'value': [1, 3, 2, 4] }
df = pd.DataFrame.from_dict(dictdata)
chart = alt.Chart(df).mark_bar().encode(
y = alt.Y('variable:N'),
x = alt.X('value:Q'),
)
chart
pn.Row(chart)
This "save button" is known as the actions menu, and is configurable in vega-embed with the actions option.
Panel's vega chart embedding explicitly removes the actions by setting {actions: false}, as seen here: panel/models/vega.ts#L71.
Unfortunately, it does not appear that the package offers any way to configure this. You might try submitting a feature request to the Panel library.

How to create a floating help layout?

I've got a {N} page and conditionally I'd like to create a floating bubble text label pointing to certain features on the page. e.g. "Press START button to blah".
Any help on how this could be done.
Preferably w/o modifying the xml page files.
I released a plugin that can do what you are requesting simply install it tns plugin add nativescript-tooltip then you can use it as follows
TypeScript
import * as frame from "ui/frame";
import {ToolTip} from "nativescript-tooltip";
new ToolTip(frame.topmost().getViewById("someView"),{text:"Some Text"});
JavaScript
const frame = require("ui/frame");
const ToolTip = require("nativescript-tooltip").ToolTip;
new ToolTip(frame.topmost().getViewById("someView"),{text:"Some Text"});
Plugin Repo

Selenium webdriver can't find button

EDIT:
I have cleaned this up a bit.
I have a button that looks like this:
<input id="applyRuleButton" class="Button" name="filtersContainer:applyRuleButton"
value="Apply" onclick="wicketShow('applyRuleButton--ajax-indicator');var
wcall=wicketSubmitFormById('id256', '?wicket:interface=:23:form:filtersContainer:applyRuleButton:
:IActivePageBehaviorListener:0:&wicket:ignoreIfNotActive=true',
'filtersContainer:applyRuleButton' ,function() { ;wicketHide('applyRuleButton--
ajax-indicator');}.bind(this),function() { ;wicketHide('applyRuleButton--
ajax-indicator');}.bind(this), function() {return
Wicket.$$(this)&&Wicket.$$('id256')}.bind(this));;; return false;" type="submit">
Firebug:
<input id="applyRuleButton" class="Button" type="submit"
onclick="wicketShow('applyRuleButton--ajax-indicator');var
wcall=wicketSubmitFormById('id2ee',
'?wicket:interface=:29:form:filtersContainer:applyRuleButton::IActivePageBehaviorListener:0
:&wicket:ignoreIfNotActive=true', 'filtersContainer:applyRuleButton' ,function() {
;wicketHide('applyRuleButton--ajax-indicator');}.bind(this),function() {
;wicketHide('applyRuleButton--ajax-indicator');}.bind(this), function() {return
Wicket.$$(this)&&Wicket.$$('id2ee')}.bind(this));;; return false;" value="Apply"
name="filtersContainer:applyRuleButton">
I'm trying to click it and have tried pretty much everything for 2 days, webdriver does not find the element, IDE does find it:
//This was my first approach, it should work.
It works in IDE, but not Webdriver:
driver.findElement(By.id("applyRuleButton")).click();
//then perhaps this should do the trick, hint: It doesn't:
WebElement element3 = driver.findElement(By.id("applyRuleButton"));
JavascriptExecutor executor3 = (JavascriptExecutor)driver;
executor3.executeScript("arguments[0].click();", element3);
Ok, Id not working, I get it.
Then this should work at least:
driver.findElement(By.xpath("//table/tbody/tr/td/div/div/table/tbody/tr[6]/td/input[#id='applyRuleButton']")).click();
It feels like I am missing something obvious here, some help please?
Additional information:
I have added a 5 second wait, the page is completely loaded.
This button is located in a table:
The Xpath is
/html/body/div[4]/div[2]/form/div[3]/div/div/table/tbody/tr/td/div/div/table/tbody/tr[6]/td/input
Webdriver error, no matter what I throw at it, is: Unable to locate element
I have used both 'click' and 'submit', still no success.
I think in this case there are two possibilities :
Either there is another element having same id/xpath.
OR Element present in another iframe.
Is the button visible. Selenium click (latest firefox 26 and latest webdriver 2.39.0) does not sometimes implicitly scroll; Or it may not scroll it fully. So scroll it into view - Scroll Element into View with Selenium and then it should work.
Note Selenium Best Practise try to use By.Id,By.CSSSelector and if nothing gets use By.Xpath in the order of priority. ( Use the FireFinder, FireBug plugin to test XPath or CSS)
This might be a synchronization issue. Such issues can be solved using smart waits.
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists((By.Id("applyRuleButton"))));
WebElement element3 = driver.findElement(By.id("applyRuleButton"));
And that should work perfectly fine.
There is absolutely nothing wrong with your selector. I just don't think you're invoking the click correctly.
Try:
driver.findElement(By.id("applyRuleButton")).click();
If this doesn't work, then you might have to invoke a WebDriverWait since you have this question marked as [ajax]
Could you post the entire html?
As a simple experiment, I took the html snippet that you posted and wrote a short python script that invokes selenium:
from selenium import webdriver
br = webdriver.Firefox()
br.get("put your path to snippet here")
button = br.find_element_by_id("applyRuleButton")
print button.get_attribute("name")
button.click()
br.close()
I can find the button and extract the attribute "name" which prints "filtersContainer:applyRuleButton". This is admittedly a very limited experiment, but it suggests that the issue is related to not being where you think you are on the page.
Try this:
driver.findElement(By.Name("filtersContainer:applyRuleButton"));
If this doesn't help, check whether this button is located in any other frame. If so, you may have to find and move your focus to that frame and then locate this button.
First try to identify the button by writting correct xpath using firebug, if you are able to identify button by that xpath then use that xpath while writing your script.
driver.findElement(By.xpath("//input[# type='submit' and # id='applyRuleButton'")).click();
This is ajax application use proper explicit/ webdriver wait till the button gets downloaded
I see that this thread is old, but I was looking at it today (Sept/2021) as I was having the same problem: I could see the name / id/ type of the button, but it would never be found.
I discovered that when I had clicked in a prior link, it opened a new tab in my browser, but Selenium did not change the focus to the new tab, so it couldn't find the ID of the button I was looking for.
I solved it with :
driver.find_element_by_id("export").click() #driver
time.sleep(2)
driver.switch_to.window(driver.window_handles[1]) # Change focus to the new tab
driver.find_element_by_id("0001btn").click() #click
driver.close() #close new tab
switching to a specific frame helped me to resolve the same issue. (python + selenium)
I installed the Selenium Recorder extension on chrome and recorded my steps, and found out that the recorder had a step to select a frame = 0, so adding
self.home_page.driver.switch_to.frame(0)
self.home_page.click_on_element_by_id("clickSubmit")
solved the problem.

Selenium Web Driver(Firefox) fails sometimes to find an element by id which is present in the web page

I am using Selenium Web Driver for Fire Fox to automate a web page inPython.
The issue is that sometime for some elements i am getting NoSuchElementException even though the element is present with the id i am searching for.
I am using find_element_by_id(id) method.
My code is
d = webdriver.Firefox()
elm5 = d.find_element_by_id("ctl00_bodyContent_tabGroupAdmin")
elm5.click()
The HTML for that element is:
<div id="ctl00_bodyContent_tabGroupAdmin" class="tab">
<div onclick="OpenTab(3); DisableProgressBarDisplay(); return false;">
Group Administrators</div>
<div class="cap_right">
</div>
</div>
Has anyone faced the same issue? Please suggest some work around.
Thanks
I would suggest that you have to wait for the element to appear.
have a look at this answer about how to wait
We had this problem, too, sometimes while using selenium. The page was too slow.
I don't know anything about Python, however i am working with selenium Webdriver using java, and i guess the issue lies bcz may you are not switched to the current frame.
You can check this by checking your html stuff by just going up and you will be able to find the current frame.
In java we used to do this as
driver.switchTo().frame(driver.findElement(By.name("officePane")));
you can check this similar in the python Hope it will help you.
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
add these to the beginning of your code and
WebDriverWait(d, 20).until(EC.presence_of_element_located((By.ID, "ctl00_bodyContent_tabGroupAdmin")))
BTW, you can find and click elements in a single line as:
d.find_element_by_id("ctl00_bodyContent_tabGroupAdmin").click()
good luck and thanks for User to teach this solution for us.
Hope your problem is fixed now. But i was facing the same problem since last 2 weeks.
Here is the below code for your problem.
It will detect the frame and locate the element specially when the element is frequently detected and some times not detected.
WebDriver d = new FirefoxDriver(); <-- Intialize your Firefox driver
WebDriverWait frame = new WebDriverWait(driver, 50); <-- Creating wait object for frame
frame.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.tagName("iframe")));
WebDriverWait wait1 = new WebDriverWait(driver, 50); <-- Creating wait object for element
wait1.until(ExpectedConditions.elementToBeClickable(find_element_by_id("ctl00_bodyContent_tabGroupAdmin"))).click();
Hope this will solve your problem if its unsolved.

Resources