Im using python and selenium, trying to open a new tab. The send_keys function is not opening the tabs but execute_script does. My issue is I have a url that is saved in a variable, and I need to pass that into the script, but I get an error.
Code:
src = 'http://yahoo.com'
driver.execute_script("window.open(" + src + ",'_blank');")
Error Message:
selenium.common.exceptions.WebDriverException: Message: unknown error: Runtime.evaluate threw exception: SyntaxError: missing ) after argument list
Also tried, does not work:
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
Does work, but url is hardcoded:
driver.execute_script("window.open('http://www.google.com/','_blank');")
You can use format to insert a variable.
An example:
driver = webdriver.Chrome(executable_path="/tmp/chromedriver")
link = 'http://example.com'
driver.execute_script('window.open("{}","_blank");'.format(link))
driver.switch_to.window(driver.window_handles[-1])
this worked:
driver.execute_script('''window.open('',"_blank");''')
driver.switch_to.window(driver.window_handles[-1])
driver.get(src)
Related
i'm new to python selenium package. I'm developing crawler for a bookie site.
I'm unable to click and open an image link.
My code:
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
web = 'https://odibets.com/'
driver.get(web)
driver.implicitly_wait(3)
# btn = driver.find_element_by_css_selector('span.icon')""
btn = driver.find_element_by_xpath("//a[#href='/League'] and //span[text()='League']")
# <img src="https://s3-eu-west-1.amazonaws.com/odibets/img/menu/odi-league-2.png">
# //img[#src ="https://s3-eu-west-1.amazonaws.com/odibets/img/menu/odi-league-2.png"]
# //span[text()='League']
btn.click()
I get the following exception.
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //a[#href='/League'] and //span[text()='League'] because of the following error: TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted to the desired type. (Session info: chrome=96.0.4664.45) Stacktrace: Backtrace: Ordinal0 [0x010D6903+2517251]
Attached is the snapshot code from chrome developer tools and page itself.
Your href was /league not /League
driver.find_element_by_xpath("//a[#href='/league'] [.//span[contains(.,'League')]]").click()
This also works somehow the element wasn't clicking correctly.
elem=driver.find_element_by_xpath("(//a[#href='/league'] [.//span[contains(.,'League')]])[1]")
driver.execute_script("arguments[0].click()", elem)
I am trying to apply subprocess.call instead of os.system following PEP 324
In a task to open multiple urls
import subprocess
open_chromes = [
'https://en.wikipedia.org/wiki/Embodied_cognition',
'https://docs.python.org/3.6/index.html',
'https://docs.djangoproject.com/en/1.11/',]
for chrome in open_chromes:
cmd = ['open', '-na', 'Google Chrome']
subprocess.call(cmd.append(chrome))
Error reported as
TypeError: 'NoneType' object is not iterable
Alternatively with os.system is definitely easy.
import os
open_chromes = [
'https://en.wikipedia.org/wiki/Embodied_cognition',
'https://docs.python.org/3.6/index.html',
'https://docs.djangoproject.com/en/1.11/',]
for chrome in open_chromes:
os.system('open -na "Google Chrome" {}'.format(chrome))
What's the problem with my code?
The append function of a list does not return anything, so subprocess.call(cmd.append(chrome)) is equivalent to subprocess.call(None), which is the problem. You need to append before doing the call
if you want to keep it a single line and not changing the original array - use + operator
subprocess.call(cmd + chrome)
I have the following code within the .rive file for RiveScript Interpreter, the code basically will say hello world when the user type the command "give me result":
> object base64 python
import base64 as b64
return b64.b64encode(" ".join(args))
< object
+ encode * in base64
- OK: <call>base64 <star></call>
Now when running that with the interpreter with the command python3 rivescript eg/brain, I get the right expected results. But when I try to run it with the following code:
from rivescript import RiveScript
bot = RiveScript()
bot.load_directory("./eg/brain")
bot.sort_replies()
while True:
msg = raw_input('You> ')
if msg == '/quit':
quit()
reply = bot.reply("localuser", msg)
print 'Bot>', reply
As it mentions here that Python support by default is on.
Edit: I forgott to mention the error I'm getting which is the following:
[ERR: Object Not Found]
Why I am getting this error?
Simple: Just reply to the bot with the trigger to that object macro.
For example: To call the object macro described include the following trigger and reply in your .rive file:
> object hello_world python
print("give me result")
< object
+ hello world
- <call>hello_world</call>
I am trying to use "${BUILD_LOG, maxLines, escapeHtml}" like discribed in:
How can I take last 20 lines from the $BUILD_LOG variable?
Unfortunately it doesn't work for me.
I get this error:
Script1.groovy: 114: expecting anything but ''\n''; got it anyway # line 114, column 301.
arted by user MYUSERNAME
My code in this line is:
msg.setText("This build (" + build.getFullDisplayName()
+ " ) contains the following tasks:\n\nTASK\t\t\t IMPLEMENTER:\n"
+ taskList + "\n\n\nLink to this
build: ${BUILD_URL} \n ${BUILD_LOG, maxLines=9999, escapeHtml=false}" );
If I take this out the following, it works. Thats why my guess is, that "BUILD_LOG" is not working anymore?
${BUILD_LOG, maxLines=9999, escapeHtml=false}
EDIT:
Maybe as addition: I am trying to do this withing the PreSend groovy script.
Since I am building the Email text dynamically.
${BUILD_URL} works fine, ${BUILD_LOG, maxLines=9999, escapeHtml=false} doesn't (for me) i am looking for a solution for this...
the msg object is a java MimeMessage.
Thanks,
Daniel
That error message is usually related to not closed quotes, comments started with / instead of //, etc. In your code the only thing I can see is that your third line is not finished properly, i.e., after "\n\n\nLink to this you are not closing double quotes and instead you are starting a new line (thereby the expecting anything but ''\n''.
Try to write the whole line:
msg.setText("This build (" + build.getFullDisplayName()
+ " ) contains the following tasks:\n\nTASK\t\t\t IMPLEMENTER:\n"
+ taskList + "\n\n\nLink to this build: ${BUILD_URL} \n ${BUILD_LOG, maxLines=9999, escapeHtml=false}" );
or close the quotes instead:
msg.setText("This build (" + build.getFullDisplayName()
+ " ) contains the following tasks:\n\nTASK\t\t\t IMPLEMENTER:\n"
+ taskList + "\n\n\nLink to this "
+ "build: ${BUILD_URL} \n ${BUILD_LOG, maxLines=9999, escapeHtml=false}" );
I used the below and it's working fine for me.
${BUILD_LOG, maxLines=10, escapeHtml=false}
I tried with Jenkins version 1.617
Have you tried to set escapeHtml=true? It may happen that this token expanded as is and then string in " " becomes not valid.
In latest version variable ${BUILD_LOG} wasn't available for me - only solution to get log in email content was for me setting:
msg.setText(build.getLog())
as Default Pre-send Script in Jenkins global configuration...
I'm trying to get a command return with selenium ide by doing it :
storeTextPresent|myText|title
gotoIf|storedVars.tite|true
echo|${"true"}
but it doesnt work...i have : [error] Unexpected Exception: fileName -> chrome://flowcontrol/content/extensions/goto-sel-ide.js?1347871647495, lineNumber -> 120.
Does anybody know how to get the return?
Thank you
I didn't check for boolean support on gotoIf, but if gotoIf wants to go to a label, I don't see a label defined in the above script.
Also, the "storedVars.tite" reference looks to be misspelled.