Python Selenium not clicking Link? - python-3.x

I am getting a list of links that I want to click. I get the href of each of them and I am able to locate the element using it, but when I try to click it, it just appears as if I were hovering on top of it.
The list is called links and the href that I am trying to access is links[1]
I have alredy tried normally clicking it:
self.driver.find_element(By.XPATH,'//*[contains(#href,links[1])]').click()
And double clicking it:
actionChains = ActionChains(self.driver)
a=self.driver.find_element(By.XPATH,'//*[contains(#href,links[1])]')
actionChains.double_click(a).perform()
But the behaviour is as if I only put the cursor on top of the link without clicking it.

just fix the xpath
driver.find_element(By.XPATH, "//*[contains(#href,'{}')]".format(links[1])).click()
or if you prefer ActionChains
actionChains = ActionChains(self.driver)
a=self.driver.find_element(By.XPATH,"//*[contains(#href,'{}')]".format(links[1]))
actionChains.double_click(a).perform()

Related

How to "scroll up" in div using selenium in python?

I am trying to scrape job details in LinkedIn. As per now, I am able to scroll till the end of list.
code:
#scrolling till buttom of page(online job section)
target = driver.find_element_by_xpath('//div[#class="jobs-search-results jobs-search-results--is-two-pane"]')
time.sleep(1)
driver.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', target)
driver.implicitly_wait(1)
time.sleep(1)
now I want to scroll up to the top of the div.
I want to scroll up in div shown as red rectangle:
Scrolling to top of the page in Python using Selenium
Scroll Element into View with Selenium
There is 2 main way to do that:
Firstly, you can locate the element(at the top of your DOM view) in the DOM and then scrolling up until you find the element
element = driver.find_element_by_xpath("element_xpath")
driver.execute_script("return arguments[0].scrollIntoView(true);", element)
Secondly, you can easily use CTRL+HOME for scrolling to the top of the page with the help of Keys
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME)
But in your situation which dealing with JavaScript DOM, recommended way is the first one.
There are many ways to do this, here is one way:
driver.execute_script("window.scrollTo(0, 0);")
Alternatively you can scroll up with the mouse:
import pyautogui
pyautogui.scroll(10)

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.

selecting item from context-menu after right click

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()

How to scrape different pages of an unchanging Url that pages change with tabs on the top bar of Url?

I want to parse this page 'http://www.tsetmc.ir/Loader.aspx?ParTree=151311&i=22811176775480091' ,but it has a tab_bar on top ,so when I click one of them it shows up new information ,but with the same url.
I am using BeautifulSoup & selenium to parse that but i can't find the tag on the pages that are shown when i click one of the tabs.
This image shows the tabs that i mean.
As far as I could see, this menu uses classes, so you have to write:
driver.find_element_by_css_selector(class="...").click()- with this one he will click on the object with the class name you put into the parentheses.
A small example:
driver.find_element_by_css_selector(class="yellow").click()
The driver would click on the yellow tab, and would open it in the same window.

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')

Resources