selecting item from context-menu after right click - python-3.x

I have a website that i want to crawl but as it is in chinese i have to firsttraslate it to english and then crawl for which i want the script to right click and open the context menu and then select translate to english. my script is able to right click and open the context menu but not able to select the translate to english option
i have used selenium webdriver for chrome along with python3.7 and have written the code accordingly for right clicking and opening the context menu but stuck on selecting the option translate to english
path_to_chromedriver = 'C:/Users/Administrator/AppData/Local/Programs/Python/Python37-32/chromedriver_new.exe'
driver =webdriver.Chrome(path_to_chromedriver)
driver.get("https://tmall.com")
your_link = driver.find_element_by_xpath('//*[#id="header"]/div/div/div/div[1]')
actionChains = ActionChains(driver)
actionChains.context_click(your_link).perform()
i expect the output to click the option translate to english from context menu

Try simulating pressing the UP key several times (in my context menu it is 4th from the bottom in order to reach "Translate to English"):
actionChains.context_click(your_link).key_down(Keys.UP).key_up(Keys.UP).key_down(Keys.UP).key_up(Keys.UP).key_down(Keys.UP).key_up(Keys.UP).key_down(Keys.UP).key_up(Keys.UP).key_down(Keys.RETURN).key_up(Keys.RETURN).perform()

Related

Click a tag to opening new tab using Selenium

I just want the relevent <td> class to click on a tag and I must do this by opening it on a new tab. Here is my code:
x1=driver.find_element_by_class_name('no').send_keys(Keys.COMMAND + 't')
Please try following :
x1=driver.find_element_by_class_name('no')
ActionChains(driver).key_down(Keys.COMMAND).click(x1).key_up(Keys.COMMAND).perform()
browser.switch_to.window(browser.window_handles[1])
One problem here is there are multiple td elements with class='no' so it is hard to tell which one you need to click. Because 2nd element in your list is highlighted, we can make the assumption you are trying to click the 2nd a link.
It sounds like you are trying to get the a element so that you can click on the link & clicking this will open a new tab. You'll need to first locate the a to click, then switch focus to the newly opened tab:
from selenium.webdriver.support.ui import WebDriverWait
# get number of currently open windows
windows_before = len(driver.window_handles)
# click link -- [2] specifies which one to click, change this to click different one
driver.find_element_by_xpath("//td[#class='no'][2]/a").click()
# wait up to 10s for new tab to open, then switch focus to new tab
WebDriverWait(driver, 10).until(lambda driver: len(windows_before) != len(driver.window_handles))
# switch to new window
driver.switch_to_window(len(driver.window_handles)-1)
The above code first stores the number of currently opened tabs using driver.window_handles. Then, once we click the 2nd a element to open event328706602.html link, we invoke WebDriverWait on number of open windows. Note that you will need to update [2] in the XPath depending on which a element you need to click.
In WebDriverWait, we wait for the number of windows to be greater than the value we previous stored, so we know the new window has been opened. Then, we switch to the new window handle by checking len(driver.window_handles) and switch to the last window_handle index the list.

PyAutoGui - locate doesn't see the right-click menu

I have the code below that basically identify the small chrome icon in the windows toolbar, right-click on it using pyautogui and then it should locate the "New Window" option. The problem I face is that, even if I take a screenshot after the right-click, the small menu doesn't show up, making it impossible to locate the "New Window" option.
# this part works
chrome_small_icon = r"C:\Users\chrome_small_icon.png"
elem = pyautogui.locateOnScreen(chrome_small_icon)
elem_center = pyautogui.center(elem)
pyautogui.click(elem_center, duration=0.5, button="right")
time.sleep(0.5)
im_after_right_click = pyautogui.screenshot()
# this part finds zero element, reason being, the right-click menu is like a ghost...
chrome_new_window = r"C:\Users\new_window_text.png"
elements = pyautogui.locateAllOnScreen(chrome_new_window)
does anybody have any suggestion about how to locate elements inside the menu that appears when we right click on an element?
Thanks
EDIT
it seems this issue happens only if I right click on the windows toolbar. It does work if I right click on other locations of the screen.
Instead of trying to locate the new window text use the keyboard to select the new window option. I just ran the following code on MacOS and I was successfully able to open a new Chrome window:
import pyautogui
import time
pyautogui.rightClick(pyautogui.center(pyautogui.locateOnScreen('chrome.png')))
#chrome.png is an image of the chrome icon
pyautogui.typewrite('new window')
pyautogui.press('enter')

Coded UI Test Builder not recognizing buttons on the pop-up window

When I click the crosshairs icon on the Coded UI Test Builder and drag it over to the pop-up window buttons (Run and Cancel), it is unable to locate and find the Properties of them on the pop-up window.
Does any one know how I can know the properties of the Run button on the pop-up window?
That window appears to be a Java window, which is not supported by Coded UI.
You may be able to extend your tests to interact with the Window:
https://blogs.msdn.microsoft.com/gautamg/2010/01/05/1-introduction-to-coded-ui-test-extensibility/
Alternatively, if you can detect if the window is present, you may be able to emulate the keypresses required to dismiss/approve the window (e.g Enter, or Tab + Enter). Or, if the window's location is predictable, emulate a click at the desired location.

Navigation in Xamarin.Form from masterdetail page

I am new to Xamarin and want to develop an Xamarin.Form Portable App.For that, I have created the project template as Xamarin.Form Portable.
After login, I have successfully created a Master Detail Page to display the Menu List like Home, AboutUs, ContactUs links in the left corner which is initially hide and when i click on the Menu Icon (Menu Icon is like three dashes(-) in parallel) all Menu List is populated to the right side in window. Now when i click on any Menu Item,I don't want to repeat the menu list or its icon in the inner pages. Instead of that, I want Navigation back arrow button instead of the Menu Icon. When I tried to navigate it like below it gives me error like "PushAsync is not supported globally on iOS/Android, please use a NavigationPage"
await Navigation.PushAsync(new AboutUs());
If instead of this, I navigate it to the master detail page and set the About Us page as Detail property of the Master Detail Page then Menu Icon and Menu list will be reapeated which i don't want. Please help me how can i show the navigate the page with Back Arrow button in inner pages.
Overall, I want functionality like in Gmail where when we open any Detail of Email Menu button is not there but the back arrow button is there.
Thanks in advance!
Try this approach(Because DetailPage must be NavigationPage in Xamarin.Forms):
await Navigation.PushAsync(new NavigationPage(new AboutUs()));

How to add custimized context menu item on right click in browser window?

Here I want to add my custom menu option i.e. ColorZilla, Aptana Studio on web browser right click
You can only do this with a browser plugin. Each browser will have its own syntax for plugins and specifically adding context menu items.
Aleks G. is right you cannot do that directly within the browser.
I don't know if it can help you or if it is what you are looking for but, within the browser you can handle the right click interaction in order to display your own custom menu ( not a particular menu item but the full menu ) ... meaning the default browser menu won't get displayed at all.
In case you are interested, you can have a small sample here

Resources