Currently using python and trying to have selenium click the "About" on google without using id. When I try to use .click() it does not execute, what is wrong with my code? I have looked at many videos and tutorials and it looks correct.
from selenium import webdriver
from time import sleep
browser = webdriver.Safari()
browser.get('http://google.com')
browser.maximize_window()
elm = browser.find_element_by_link_text('About')
browser.implicitly_wait(5)
elm.click()
I think, you can try using find_element_by_xpath.
First you will copy xpath of about link then you can try like below:
from selenium import webdriver
from time import sleep
browser = webdriver.Safari()
browser.get('http://google.com')
browser.maximize_window()
elm = browser.find_element_by_xpath('//*[#id="fsl"]/a[3]')
browser.implicitly_wait(5)
elm.click()
So the issue ended up being safari. For some reason the web driver safari was not allowing me to use .click. I switched to chrome and it worked.
Related
I am trying to get in this website: "https://core.cro.ie/".
I can get in using normal web search, but I can't get in using selenium.
My code looks like this:
site= "https://core.cro.ie/"
driver = webdriver.Edge(service=Service(EdgeChromiumDriverManager().install()))
driver.get(site)
driver.maximize_window()
Any ideas? Thank you very much
This code works fine for navigation ( I dont have Edge browser):
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
site= "https://core.cro.ie/"
driver = webdriver.Firefox()
driver.get(site)
driver.maximize_window()
I have installed selenium prior running the test. It seems like the website has some sort of bot prevention mechanism but navigation works fine:
pip install selenium
I'm new to selenium, this is my first day of learning it so please go easy on me.
So I want to print umidTokenFromHeader a javascript variable of this website https://member.lazada.co.id/user/login. Here's my code so far
#!/usr/bin/python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://member.lazada.co.id/user/login")
# print (umidTokenFromHeader)
driver.close()
On the Console tab of Chrome Developer Tools, umidTokenFromHeader looks like this:
I found this question Reading JavaScript variables using Selenium WebDriver
but I don't understand Java at all
How do I do this on selenium with python ?
You can use a JavaScript and execute_script() to return the value of umidTokenFromHeader variable. Then you can capture that value in element variable and finally print it.
#!/usr/bin/python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://member.lazada.co.id/user/login")
element = driver.execute_script("return umidTokenFromHeader")
print (element)
driver.close()
When use code below I have a Error with selenium over PhantomJS find_by_xpath in google search result:
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.get('https://www.google.com/search?q=test')
t = driver.find_element_by_xpath("//li[8]/div/h3/a").text
print(t)
driver.quit()
Error:
https://36.media.tumblr.com/df9020dcb2a68b969bc8097ceccc187e/tumblr_nkqcdokxnu1r68ev5o1_540.png
in bing
from selenium import webdriver
driver = webdriver.PhantomJS()
#driver.get('https://www.google.com/search?q=test')
driver.get('http://global.bing.com/search?q=test')
#driver.find_element_by_xpath("//li[8]/div/h3/a").click()
t = driver.find_element_by_xpath("//li[3]/h2/a").text
print(t)
driver.quit()
It's all Right.
xpath1:"//li[8]/div/h3/a" and "//li[3]/h2/a" are both Right in Xpath.
any idea about google search.
Try to use some wait , might be possible element is not available when you are trying to access it. I tried this its working fine and giving this as output:
XFINITY Speed Test
I just want to refresh an already opened web page with Selenium.
It always opens a new browser window.
What I'm doing wrong?
from selenium import webdriver
import urllib
import urllib2
driver = webdriver.Firefox()
driver.refresh()
I would suggest binding the driver element search to the tag body and use the refresh command of the browser.
In OSX for example
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'r')
Documentation on keys here: http://selenium-python.readthedocs.org/en/latest/api.html
Update:
The following code, very similar to your one, works fine for me.
driver = webdriver.Firefox()
driver.get(response.url) #tested in combination with scrapy
time.sleep(3)
driver.refresh()
Are you sure you correctly load the web page with the driver before refreshing it ?
The problem is you are opening the webdriver and then trying to refresh when you have not specified a URL.
All you need to do is get your desired URL before refreshing:
from selenium import webdriver
import urllib
import urllib2
driver = webdriver.Firefox()
driver.get("Your desired URL goes here...")
#now you can refresh the page!
driver.refresh()
The following codes work for me
driver.get(driver.current_url)
sleep(2)
driver.refresh()
I use python 3.7.6, selenium 3.141.0
You are trying to refresh the page before it loads so u can use a sleep function
from time import sleep
sleep(1)
or you can wait for an XPath to load so
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, xpath goes here)))
For me helped
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get("URL")
time.sleep(5)
driver.refresh()
I got mine fixed by adding "browser.refresh()" the for loop or while loop.
You can try any one of the below methods for the same.
Method 1:
driver.findElement(By.name("s")).sendKeys(Keys.F5);
Method 2:
driver.get(driver.getCurrentUrl());
Method3:
driver.navigate().to(driver.getCurrentUrl());
Method4:
driver.findElement(By.name("s")).sendKeys("\uE035");
I am having a strange issue with PhantomJS or may be I am newbie. I am trying to login on NewEgg.com via Selenium by using PhantomJS. I am using Python for it. Issue is, when I use Firefox as a driver it works well but as soon as I set PhantomJS as a driver it does not go to next page hence give message:
Exception Message: u'{"errorMessage":"Unable to find element with id \'UserName\'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"89","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:55372","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\\"using\\": \\"id\\", \\"sessionId\\": \\"aaff4c40-6aaa-11e4-9cb1-7b8841e74090\\", \\"value\\": \\"UserName\\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/aaff4c40-6aaa-11e4-9cb1-7b8841e74090/element"}}' ; Screenshot: available via screen
The reason I found after taking screenshot that phantom could not navigate the page and script got finished. How do I sort this out? Code Snippet I tried given below:
import requests
from bs4 import BeautifulSoup
from time import sleep
from selenium import webdriver
import datetime
my_username = "user#mail.com"
my_password = "password"
driver = webdriver.PhantomJS('/Setups/phantomjs-1.9.7-macosx/bin/phantomjs')
firefox_profile = webdriver.FirefoxProfile()
#firefox_profile.set_preference('permissions.default.stylesheet', 2)
firefox_profile.set_preference('permissions.default.image', 2)
firefox_profile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')
#driver = webdriver.Firefox(firefox_profile)
driver.set_window_size(1120, 550)
driver.get('http://newegg.com')
driver.find_element_by_link_text('Log in or Register').click()
driver.save_screenshot('screen.png')
I even put sleep but it is not making any difference.
I experienced this with PhantomJS when the content type of the second page is not correct. A normal browser would just interpret the content dynamically, but Phantom just dies, silently.