[英]Python Selenium WebElement element - invalid syntax
I'm trying to scroll to a particular element on a page but before I do that I'm trying to set myelement to be a WebElement object, heres the code: 我试图滚动到页面上的特定元素,但在此之前,我试图将myelement设置为WebElement对象,下面是代码:
from selenium import webdriver
browser.get("http://www.agoda.com/the-coast-resort-koh-phangan/hotel/koh-phangan-th.html")
WebElement myelement = browser.findElement(By.id("next-page"));
But it comes back with the error: 但是它返回了错误:
WebElement myelement = browser.findElement(By.id("next-page"));
^
SyntaxError: invalid syntax
What am I missing? 我想念什么?
Update: Ok looks like I was trying to write Java! 更新:好的,看来我正在尝试编写Java! as the first reply has stated, but then my question kind of changes since my actual code is:
如第一个答复所述,但是由于我的实际代码,我的问题有所改变:
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Firefox()
browser.get("http://www.agoda.com/the-coast-resort-koh-phangan/hotel/koh-phangan-th.html")
browser.find_element_by_xpath("//a[@id='next-page']").click()
myelement = browser.find_element(By.id("next-page"));
((JavascriptExecutor) browser).executeScript("arguments[0].scrollIntoView(true);", element);
Thread.sleep(500);
browser.find_element_by_xpath("//a[@id='next-page']").click()
Which gives the error: 给出错误:
File "<ipython-input-22-bb983f3ceca8>", line 10
((JavascriptExecutor) browser).executeScript("arguments[0].scrollIntoView(true);", element);
^
SyntaxError: invalid syntax
Probably writing Java again, but how should this be amended? 可能再次编写Java,但是应该如何修改呢?
Python isn't Java. Python不是Java。 (WebElement element = ...;)
(WebElement元素= ...;)
Here's a link to the documentation for the python methods etc. 这是python方法等文档的链接。
What you actually want is: 您真正想要的是:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get("http://www.agoda.com/the-coast-resort-koh-phangan/hotel/koh-phangan-th.html")
myelement = browser.find_element_by_id("next-page")
Although you can use By
in the above but find_element_by_id
is a bit more readable imo. 尽管您可以在上面使用
By
,但是find_element_by_id
在imo中更具可读性。
Edit: 编辑:
Likewise for the second bit 同样对于第二位
browser.execute_script("arguments[0].scrollIntoView();", element)
I haven't used the above in awhile, so it may actually be the following I don't recall atm: 我已经有一段时间没有使用上面的了,所以实际上可能是以下我不记得的atm:
browser.execute_script("return arguments[0].scrollIntoView();", element)
You can also scroll to an element like this: 您也可以滚动到这样的元素:
browser.execute_script("window.scrollTo(0, %d)" % element.location['y'])
Thread.sleep(500);
also isn't going to work, and you can remove the semicolons ;
也行不通,您可以删除分号
;
after your lines, they're not necessarily "wrong" but they are redundant. 在您的台词之后,它们不一定是“错误的”,但它们是多余的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.