How to click on a context menu- popup menu - python

I need to do the following:
1) Right click on a element
2) Context menu is displayed
3) On moving
to a specific menu (move_to_element) in context menu - pops up
another menu say MenuX
4) Need to click on MenuX
I am able to do steps 1 to 3 but not 4.
When I checked is_displayed for MenuX it returns False
When I try driver.find_element_by_xpath("html/body/div[5]/span[2]")
it works (I do not want to hardcode).
But not with id of menu.
Also tried but no luck
actions = ActionChains(driver)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()
I also tried driver.find_element_by_css_selector("div#contextMenu_Div #menuX") and it didn't work.
MenuX is not link but a span element.

Try to use below code in order to click on the sub menu item after right clicking:
WebElement element = driver.findElement("Find the element on which right click needs to be performed"));
Actions actions = new Actions(driver).contextClick(element);
WebElement mainMenu = driver.findElement(By.linkText("menulink"));
actions.moveToElement(mainMenu);
WebElement menuX = driver.findElement(By.cssSelector("subLinklocator"));
actions.moveToElement(menuX);
actions.click().build().perform();
Hope this helps

Can you try this..
Actions actions = new Actions(driver);
actions.contextClick(mainelement);
actions.moveToElement(menuelement);
actions.perform();
//at this point ur MenuX shd be visisble.
driver.findElement("locator for ur MenuX").click();

Related

How to click on the highlighted button in selenium ide

[![***
***]2][1]I'm working with selenium ide and I want to click on the button that is highlighted in the calendar that shows today
I have found an order in Selenium that can return the date of the day to me, but I want the date in the Persian solar calendar.
var d = new Date();
var m = ((d.getMonth()+1)<10)?'0'+(d.getMonth()+1):
(d.getMonth()+1);
return d.getFullYear()+"-"+m+"-"+d.getDate();
I want to know if it is possible to click on a button highlighted with selenium ide
First, you can always click on elements by the text inside them using Xpath:
String xpathVal = "*//[contains(text()," + "'TEXT')]";
WebElement myElement = driver.findElementByXpath(xpathVal);
myElement.click();
Also, you can click on the dropdown (select) and do this:
Select select = (Select)driver.findElement(By.id
("ID of select element"));
select.selectByValue("optionText");
// or
select.selectByIndex(2);

How to locate the element in this?

I am new to Selenium programming and have an issue.
z=driver.find_elements_by_xpath("/html/body/div[3]/div/div[3]/div/div[2]/div/div/div[2]/div/table[1]/tbody/tr/td[2]/div/div[3]/input[1]")
text = input("Gebietsdefinition: ")
z[0].send_keys(text)
time.sleep(2)
xpath : /html/body/div[3]/div/div[3]/div/div[2]/div/div/div[2]/div/table[1]/tbody/tr/td[2]/span
id : dijit_form_Button_18
class : dijit dijitReset dijitInline SendRequest dijitButton
this is the full xpath, class & id of the button. But when I use the command,
driver.find_element_by_id("dijit_form_Button_18").click()
it executes in python without any error, but the webdriver stays the same.
NB: The button changes its class when I move the cursor over the button to "Hover" and changes to "Button ActiveFocused" when I click.
I hope somebody can shed some light into this. Thank you in advance.
Basically, you are tying to say that no error comes and click effect is also not visible.
Most probably, Selenium is clicking the element before it is clickable that is before the element is enabled to be clicked.
You can:
Introduce some delay in between using wait
Or you can use webdriverwait class and that will help
Without looking at the HTML with you, I hope that I can answer your population question. For the population question, you should import the following
from selenium import webdriver
from selenium.webdriver.common.by import By
and then you can try to populate your textbox with the following steps
element = driver.find_element(By.XPATH, "Your xpath here")
customText = "Your text here"
element.send_keys(customText)
# Gets the text that displays inside of our textbox after population
inputText = driver.find_element(By.XPATH, "Your xpath here").get_attribute('value')
if inputText.lower() != customText.lower():
raise Exception("Textbox failed to populate")
If the input tag is a button and has the following class elements
dijit dijitReset dijitInline SendRequest dijitButton
then you can use the following xpath to get your button
//input[contains(#class, 'SendRequest') and contains(#class, 'dijitButton')]
and then click it by using the following command
driver.find_element(By.XPATH, "//input[contains(#class, 'SendRequest') and contains(#class, 'dijitButton')]").click()
I hope this helps point you in the right direction.

Element is not clickable (the button is blocking by other element)

I'm trying to click on this button: browser.find_element_by_id('btnSearch')
But this button is blocking by this div tag: <div id="actionSearch" class="row pull-right">
How do I go around to click this button with id='btnSearch" while it's blocking by the actionSearch div?
I tried the following:
browser.find_element_by_id('btnSearch').click()
browser.implicitly_wait(10)
el = browser.find_element_by_xpath('//*[#id="btnSearch"]')
ActionChains(browser).move_to_element_with_offset(el, 1827, 270)
ActionChains(browser).click()
ActionChains(browser).perform()
element = browser.find_element_by_id('btnSearch')
browser.execute_script("arguments[0].click();", element)
wait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, //*[#id="btnSearch"]'))).click()
none of these work.
Can anyone help me with this? I've spent two days trying to click this button!! Please help!
Considering provided image of HTML source (tip: do not provide it as image, but as text instead) I can assume that required element located in the bottom of page and you might need to scroll page down to be able to handle it.
Try below code
link = browser.find_element_by_id("btnSearch")
browser.execute_script("arguments[0].scrollIntoView()", link)
link.click()
Note that link is not a pseudo-element (is not located inside ::before/::after pseudo-element), so it can not be the cause of your problem
As for your code:
ActionChains(browser).move_to_element_with_offset(el, 1827, 270)
ActionChains(browser).click()
ActionChains(browser).perform()
Here you're trying to make scroll to link with huge offset and then you make click on current mouse position - not on link
You can try to modify it as
ActionChains(browser).move_to_element(el)
ActionChains(browser).click(el) # Pass WebElement you want to click as argument to `click()`
ActionChains(browser).perform()

How click on right click menu in selenium python?

I am trying to click on right click menu and i am able to open the right click menu but its not clicking on any menu or not pressing any keys like down , up etc where i am wrong :
profile_pic=driver.find_element_by_css_selector('img[class="profilePic img"]')
okl=profile_pic.click()
time.sleep(5)
action1=ActionChains(driver)
action1.context_click(okl).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ENTER).perform()
here is a working version to do it:
first right click to show up the menu
menuDiv = browser.find_element_by_xpath("//<selector>']")
actionChains.move_to_element(menuDiv).perform()
actionChains.context_click().perform()
then locate the submenu item you want to click on
time.sleep(3) //better wait for a little while
targetMenu = browser.find_element_by_xpath("//<selector>")
actionChains.click(targetMenu)
actionChains.perform()

Python+Selenium, can't click the 'button' wrapped by span

I am new to selenium here. I am trying to use selenium to click a 'more' button to expand the review section everytime after refreshing the page.
The website is TripAdvisor. The logic of more button is, as long as you click on the first more button, it will automatically expand all the review sections for you. In other words, you just need to click on the first 'more' button.
All buttons have a similar class name. An example is like taLnk.hvrIE6.tr415411081.moreLink.ulBlueLinks. Only the numbers part changes everytime.
The full element look like this:
<span class="taLnk hvrIE6 tr413756996 moreLink ulBlueLinks" onclick=" var options = {
flow: 'CORE_COMBINED',
pid: 39415,
onSuccess: function() { ta.util.cookie.setPIDCookie(2247); ta.call('ta.servlet.Reviews.expandReviews', {type: 'dummy'}, ta.id('review_413756996'), 'review_413756996', '1', 2247);; window.location.hash = 'review_413756996'; }
};
ta.call('ta.registration.RegOverlay.show', {type: 'dummy'}, ta.id('review_413756996'), options);
return false;
">
More </span>
I have tried several ways to get the button click. But since it is an onclick event wrapped by span, I can't successfully get it clicked.
My last version looks like this:
driver = webdriver.Firefox()
driver.get(newurl)
page_source = driver.page_source
soup = BeautifulSoup(page_source)
moreID = soup.find("span", class_=re.compile(r'.*\bmoreLink\b.*'))['class']
moreID = '.'.join(moreID[0:(len(moreID)+1)])
moreButton = 'span.' + moreID
button = driver.find_element_by_css_selector(moreButton)
button.click()
time.sleep(10)
However, I keep getting the error message like this:
WebDriverException: Message: Element is not clickable at point (318.5,
7.100006103515625). Other element would receive the click....
Can you advise me on how to fix the problem? Any help will be appreciated!
WebDriverException: Message: Element is not clickable at point (318.5, 7.100006103515625). Other element would receive the click....
This error to be occur when element is not in the view port and selenium couldn't click due to some other overlay element on it. In this case you should try one of these following solution :-
You can try using ActionChains to reach that element before click as below :-
from selenium.webdriver.common.action_chains import ActionChains
button = driver.find_element_by_css_selector(moreButton)
ActionChains(button).move_to_element(element).click().perform()
You can try using execute_script() to reach that element before click as :-
driver.execute_script("arguments[0].scrollIntoView(true)", button)
button.click()
You can try using JavaScript::click() with execute_script() but this JavaScript::click() defeats the purpose of the test. First because it doesn't generate all the events like a real click (focus, blur, mousedown, mouseup...) and second because it doesn't guarantee that a real user can interact with the element. But to get rid from this issues you can consider it as an alternate solution.
driver.execute_script("arguments[0].click()", button)
Note:- Before using these options make sure you're trying to interact with correct element using with correct locator, otherwise WebElement.click() would work well after wait until element visible and clickable using WebDriverWait.
Try using an ActionChains:
from selenium.webdriver.common.action_chains import ActionChains
# Your existing code here
# Minus the `button.click()` line
ActionChains(driver).move_to_element(button).cli‌​ck().perform()
I have used this technique when I need to click on a <div> or a <span> element, rather than an actual button or link.

Categories

Resources