I want to click on an element(found by xpath) but if i use:
ActionChains(driver).key_down(Keys.CONTROL).move_to_element(myElement).click().key_up(Keys.CONTROL).perform()
-> I get a new tab but the current tab also Redirects to the page
So what I need is a way to perform a right click and choose "open in new tab" but that does not work for me.
I use this code but after I get into the context_menu nothing happens.
ActionChains(driver).context_click(myElement).send_keys(Keys.DOWN).send_keys(Keys.RETURN).perform()
Another way would be to click "myElement" with the scroll wheel button but i can not find the click method for that.
Thank you.
(I do not need anything with Control + mouse click :-) )
If the trouble is managing the window/tab focus, you could use window_handles as in this post ?
I didn't understand if you wanted to get control over previous tab or the next one. For the first tab, that would be something like
driver.switch_to_window(driver.window_handles[1])
Edit : try this (3 steps, requiring first to acquire the target, then opening a blank new tab, then reaching target)
from selenium.webdriver.common.keys import Keys
href = myElement.get_attribute("href")
current_page = driver.find_element_by_tag_name('body')
current_page.send_keys(Keys.CONTROL + 't') #open new tab
driver.switch_to_window(driver.window_handles[1])
driver.get(href)
Related
selenium/chromedriver
When clicking a button, a new tab is opened when I do it through a GUI browser.
Python Selenium seems to have no problem clicking the button, as it gives me no errors. The errors come in the next step, when I need to find an element in the clicked page. I had selenium take a screenshot and it still shows the first page.
Presumably it clicked the button, created a new tab, and didn't switch over?
How do I switch to the new tab, or even verify the new tab exists in the first place?
Thank you!
When you open a browser with selenium, it stores a handle for each tab/window it is controlling in a list called window_handles, for example:
from selenium import webdriver
driver = webdriver.Firefox()
print(driver.window_handles)
...should give you something like ['6b7af9bb-f299-462e-a79a-2b8fda63f388']
When you open a new tab, a new handle for that window/tab should be added to that list. To then switch to the tab you want, use driver.switch_to.window(), for example (continuing above example):
driver.switch_to.window(driver.window_handles[1])
Note: you could also use driver.switch_to_window, but this is deprecated in favor of the above example.
Also, just a tip for debugging, it can be helpful to use the python repl so you can follow what the browser is doing in real time.
So I am trying to write a code to automatically join a google meet. So I need to sign into my google account, which runs all fine but when I get to "Verify that it's you" (image 1), and I try to use driver.get it takes me back to the original sign-in page (image 2)
How can I click on the "Continue" button without using driver.get/without getting taken back to the original sign-in page. I know that all I have to do is click on the "Continue" because when I do it manually it works perfectly. Pressing "Tab" and then "Enter" would also work, but it seems you need to use driver.get as well. Thank you
so i think you don't understand driver.get() usage. it is used only to make the browser browse to a specific link which you can specify before hand. what you have to do here is open the google meet page manually and go to your required page where you want to click the button. right click on the button and click on inspect. then go to the higlighted code and locate the button you want and right click on copy and select xpath from the list. now coming to your code.
add this line:
element=driver.find_element_by_xpath('_xpath')
element.click()
replace _xpath with the long string you copied (which is the xpath of the button you want to click).
this is the way to click on buttons or text boxes or anything you name it in selenium.
**beware of one thing. don't make your code click immediately. if the page does not fully load and the click is made then the 1st line will throw an element not found error. put some kind of delay till page loads or use a while loop and exception handling to wait till the element is found
This one should work for you
userName = driver.find_element_by_xpath("//button[#name='username']")
driver.execute_script("arguments[0].click();", userName)
I am working on a selenium project in Python where I am automating an online product ordering from https://www.flipkart.com/. I searched for a product(protein supplement in my case). Now when I am clicking on that product, details are now opening in a new tab with buying option there. But I don't have access to the DOM of that tab because I was working with the main page only. I am attaching two images along with code so that you can get my point.enter image description here
This is my code where I automated the process to click on the product-
def select_product(self):
element = self.find_elements_by_css_selector("div[class='_4ddWXP']")
self.refresh()
try :
element[0].click()
except StaleElementReferenceException :
element = self.find_elements_by_css_selector("div[class='_4ddWXP']")
element[0].click()
Please tell me how to proceed further. My main aim is to order that product.
You need to switch to new tab in order to proceed further. The below code you can write once you click on product which will land you on new tab.
all_handles = driver.window_handles
driver.switch_to.window(all_handles[1])
new tab will have different handle, so Selenium has to switch to new tab.
I want to login to a site. Right-click on one of the links and open in New Tab or New Window.
I have searched earlier here and googled around before posting it here. May be I am doing it wrong
button=browser.find_element_by_link_text('Menus');
action=ActionChains(browser)
action.context_click(button).perform() #--> Till here working fine, Right clicks on Menu
action.send_keys(Keys.ARROW_DOWN+Keys.ARROW_DOWN+Keys.ENTER).perform() #--> Not Working
I wouldn't go into that direction as performing context menu clicks will bite you back when you'll be running your Selenium tests in Parallel Mode
Instead of opening context menu and clicking I would rather recommend:
Extract href attribute from the link
Use Window.open() JavaScript function to open the link in the new tab
Use Explicit Wait to wail until number of windows becomes 2
Switch the context to the new tab
Example code:
button = browser.find_element_by_link_text('Menus')
href = button.get_attribute("href")
browser.execute_script("window.open('" + href + "')")
WebDriverWait(browser, 10).until(EC.number_of_windows_to_be(2))
browser.switch_to.window(browser.window_handles[1])
I'm writing a scraper that requires selecting a link to open a new window. To activate the window and check that it is activated, I use :
driver.switch_to_window(driver.window_handles[1])
driver.title
print title
It worked once and then never again. I did some other check to make certain it was recognizing the windows existence and it does, yet it will not switch:
print len(driver.window_handles)
print driver.window_handles
I'm using the following website:
chromedriver = 'C:\Python27\drivers\chromedriver'
driver = webdriver.Chrome(chromedriver)
driver.get("https://ccrecordse.tarrantcountytx.gov/RealEstate/SearchEntry.aspx")
The program enters dates + lease documents, moves to the next page, clicks the document icons to open the new window, but will not switch to the new window. I cannot figure out why.
Thanks for your help and let me know if I need to provide more information in my question!
I opened the window by using get_element_by_id.click(). But it seems I was capturing the change incorrectly, and if I print driver.title, it shows the correct handler.
Thank you!