[英]What is the best way to click on a tab using Java and selenium on a webpage?
I am new to Selenium but I have a question.我是 Selenium 的新用户,但我有一个问题。 On the URL
上URL
I want to click the tab "Nyckeltal" (at top of the page) with the我想单击选项卡“Nyckeltal”(位于页面顶部)
element:元素:
<a class="instrument-table-tab-menu__tab js_tab-menu__tab" data-target="table_2">Nyckeltal</a>.
Currently I can click the tab using the code:目前我可以使用代码单击选项卡:
String xpath ="/html/body/div/div[4]/div/div[4]/div[1]/div[1]/nav/a[3]";
driver.findElement(By.xpath(xpath)).click();
But I guess there is a better way to do this?但我想有更好的方法吗? I guess the path is regular changed because of ads and therefor I think using xpath is not particular good.
我猜路径经常因为广告而改变,因此我认为使用 xpath 不是特别好。
My question is there a better way to click "Nyckeltal" than using xpath with Selenium and if there is, how do I write?我的问题是有没有比使用 xpath 和 Selenium 更好的点击“Nyckeltal”的方法,如果有,我该如何写?
You can use below xPath
to click that tab.您可以使用下面
xPath
单击该选项卡。
//a[contains(@class,'instrument-table-tab-menu') and text()='Nyckeltal']
In these type of cases, you need to create a dynamic xPath
that means same xPath
can be used for multiple element with different values.在这些类型的情况下,您需要创建一个动态的
xPath
,这意味着相同的xPath
可用于具有不同值的多个元素。
Examples : In below examples I have just changed the text of the tab.示例:在下面的示例中,我刚刚更改了选项卡的文本。
xPath
for Kurser
tab. xPath
用于Kurser
选项卡。
//a[contains(@class,'instrument-table-tab-menu') and text()='Kurser']
xPath
for Historik
tab. Historik
选项卡为xPath
。
//a[contains(@class,'instrument-table-tab-menu') and text()='Historik']
I would suggest you to practice with xPath
more.我建议你多练习
xPath
。 Please go through this tutorial请go通过本教程
Very easy to do if you have the class name with:如果您的 class 名称为:
driver.findElement(By.className("PUT CLASSNAME OF ELEMENT HERE")).click();
You should give more precedence to css selector than xpath你应该给予 css 选择器比 xpath 更多的优先级
CSS
would be: CSS
将是:
a[class^='instrument-table-tab-menu'][data-target$='2']
XPATH
would be: XPATH
将是:
//a[contains(@class,'instrument-table-tab-menu') and contains(@data-target,'2')]
There are basically 4 ways to click in Selenium. Selenium里面基本上有4种点击方式。
Using XPATH使用 XPATH
Code trial 1:代码试用1:
time.sleep(5)
driver.find_element_by_xpath("//a[contains(@class,'instrument-table-tab-menu') and contains(@data-target,'2')]").click()
Code trial 2:代码试用2:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@class,'instrument-table-tab-menu') and contains(@data-target,'2')]"))).click()
Code trial 3:代码试用3:
time.sleep(5)
button = driver.find_element_by_xpath("//a[contains(@class,'instrument-table-tab-menu') and contains(@data-target,'2')]")
driver.execute_script("arguments[0].click();", button)
Code trial 4:代码试用4:
time.sleep(5)
button = driver.find_element_by_xpath("//a[contains(@class,'instrument-table-tab-menu') and contains(@data-target,'2')]")
ActionChains(driver).move_to_element(button).click().perform()
Imports:进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.